• Android EditText部分特殊功能


    1、给EditText加上文字选中功能,比如微博的插入话题功能。点击“插入话题”按钮的时候,“#请插入话题名称#”在两个#号中间的内容处于选中状态,用户一点击即消失。

    代码如下:

    Java代码
       
    text.setText("#请插入话题名称#");     
    Editable editable = text.getText();     
    Selection.setSelection(editable, 1, editable.length() - 1); 


    2、如果想默认进入一个Activity时,唯一的一个edittext先不要获得焦点。

    在EditText前面加上一个没有大小的Layout:

    XML/HTML代码
       
    <LinearLayout     
        android:focusable="true" android:focusableInTouchMode="true"     
        android:layout_width="0px" android:layout_height="0px"/> 

    3、输入文字的时候,如果想限制字数,并提示用户,可用以下方法:

    Java代码
       
    text.addTextChangedListener(new TextWatcher() {     
                     
                @Override     
                public void onTextChanged(CharSequence s, int start, int before, int count) {     
                     textCount = textCount + count - before;     
                     if (textCount <= 140) {     
                           writeWordDes.setText("可输入字数:" + (140 - textCount));     
                           writeWordDes.setTextColor(getResources().getColor(R.color.solid_black));     
                     } else {     
                         writeWordDes.setText("超出字数:" + (textCount - 140));     
                         writeWordDes.setTextColor(getResources().getColor(R.color.solid_red));     
                     }     
                }     
                     
                @Override     
                public void beforeTextChanged(CharSequence s, int start, int count,     
                        int after) {     
                         
                }     
                     
                @Override     
                public void afterTextChanged(Editable s) {     
                         
                }     
            });     
        } 

    4、让EditText不可输入,比如超过一定字数后,不让用户再输入文字:

    Java代码
       
    text.setFilters(new InputFilter[] {       
             new InputFilter() {       
                    public CharSequence filter(CharSequence source, int start,       
                            int end, Spanned dest, int dstart, int dend) {       
                        return source.length() < 1 ? dest.subSequence(dstart, dend) : "";       
                    }       
                }       
            });


    TextView文字跑马灯效果

    XML/HTML代码
       
    <TextView     
        android:id="@+id/textView1"     
        android:layout_width="150dp"     
        android:layout_height="wrap_content"     
        android:ellipsize="marquee"     
        android:focusable="true"     
        android:focusableInTouchMode="true"     
        android:marqueeRepeatLimit="marquee_forever"     
        android:scrollHorizontally="true"     
        android:text="这是文字跑马灯效果,滚动了么?"     
        android:textColor = "@android:color/white" /> 

    注意 若是android:layout_width="wrap_content",就没有跑马灯的效果.

    在TextView 中经常引用string.xml里的内容,string里一些常用的表示:

    <b></b>加粗字体

    <i></i> 斜体字体

    <u></u> 给字体加下划线

    \n 换行

    \u0020表示空格

    \u2026表示省略号

    <       &lt;

    >              &gt;

    &              &amp;

    ' (单引号)   &apos;

    "(双引号)   &quot;
     

    String 里%1$s,%2$d 是格式化字符串,%1$s是个字符串 %2$d 是decimal number;例如:

    XML/HTML代码
       
    <string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>  
    Java代码
       
            Resources res = getResources();       
            String text = String.format(res.getString(R.string.welcome_messages), "World", 8);  

    打印出来是: text--==>Hello, World! You have 8 new messages.
    关于String里的具体可以看api:http://developer.android.com/guide/topics/resources/string-resource.html

     在TextView中也可以显示html的效果:


    Java代码
       
    textView.setText(Html.fromHtml("Hello <b>World</b>,<font size=\"3\" color=\"red\">Hello World!</font>"));


    Dialog 去白色边框及透明

    在style.xml中
     

    XML/HTML代码
       
    <?xml version="1.0" encoding="utf-8"?>     
    <resources>     
     <style name="dialog" parent="@android:style/Theme.Dialog">     
      <item name="android:windowFrame">@null</item>     
      <item name="android:windowIsFloating">true</item>     
      <item name="android:windowIsTranslucent">true</item>     
      <item name="android:windowNoTitle">true</item>     
      <item name="android:background">@null</item>     
      <item name="android:windowBackground">@null</item>     
      <item name="android:backgroundDimEnabled">false</item>     
     </style>     
         
    </resources> 

    在代码中

    Java代码
       
    Dialog dialog = new Dialog(XXX.this,R.style.dialog);      
                    dialog.setContentView(showPassView);      
                    dialog.getWindow().setBackgroundDrawableResource(      
                            R.color.translucent_background);      
                    dialog.show(); 
    XML/HTML代码
       
    <color name="translucent_background">#A0000000</color> 

    color 值可根据实际需要去调整

    Android TextView中设定个别文字字体显示格式

      用Html来格式化字符,例如要实现如下的显示:
    "这只是一个测试字符串,测试黑体字、斜体字、下划线、红色字的显示。"

    String source = "这只是一个测试字符串,测试<b>黑体字</b>、<i>斜体字</i>、<u>下划线</u>、<font color='red'>红色字</font>的显示。"; 
    然后调用TextView里面setText函数即可
    textView.setText(Html.fromHtml(source)); 

    android学习之 如何在当前的Activity退出程序而不是关闭当activity

    关闭当前activity是用finish()方法

    那如何在当前activity下关闭程序

    方法如下:

      Java代码
      final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
      am.restartPackage(getPackageName());
      再加上uses-permission
      Xml代码
      <uses-permission android:name="android.permission.RESTART_PACKAGES"></uses-permission>

    设置无标题的activity

    在setContentView函数前加上 这句就行了 requestWindowFeature(Window.FEATURE_NO_TITLE);

  • 相关阅读:
    .Net工具 SocanCode代码生成器
    .net开发中两个“属性”引起的歧异
    读取Excel文件时出现null的解决方法
    Spring中XML配置的12个技巧
    oracle中的NVL,NVL2,NULLIF,COALESCE几个通用函数
    VB6各数据类型序列化和反序列化
    Oracle分页查询语句(四)
    ASP.NET知识库
    你知道.NET框架下的自动内存管理吗?
    构建支持 Ajax 的自动完成和级联式下拉控件
  • 原文地址:https://www.cnblogs.com/crazywenza/p/2793022.html
Copyright © 2020-2023  润新知