天天看点

Android中的文本输入

1、如何编写带有超链接的TextView

首先在XML中添加一个TextView:

<TextView
            android:id="@+id/telcel_alert_infos"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="20dp"/>
           

然后在Java代码中对这个TextView加入超链接:

TextView textView = (TextView) findViewById(R.id.telcel_alert_infos);
        String webLinkText = getString(R.string.telcel_alert_infos);
        SpannableString sp = new SpannableString(webLinkText);
        sp.setSpan(new URLSpan("http://www.internet.telcel.com/"), , ,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //对75到94的字符进行超链接到地址:http://www.internet.telcel.com/
        textView.setText(sp);
        textView.setMovementMethod(LinkMovementMethod.getInstance()); //这句一定要加
           

strings.xml中定义的字符串:

<string name="telcel_alert_infos">Algunas aplicaciones requieren conexión a datos. Te sugerimos contratar un paquete de Internet. Detalles con tu Operador de servicio.</string>
           

扩展:

还可以修改TextView中的字体颜色,背景颜色等。

TextView textView = (TextView) findViewById(R.id.telcel_alert_infos);
        String webLinkText = getString(R.string.telcel_alert_infos);
        SpannableString sp = new SpannableString(webLinkText);
        sp.setSpan(new URLSpan("http://www.internet.telcel.com/"), , ,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//对75到94的字符进行超链接到地址:http://www.internet.telcel.com/
  sp.setSpan(new BackgroundColorSpan(Color.RED), , , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //从17到19的字符,设置背景色为红色
        sp.setSpan(new ForegroundColorSpan(Color.YELLOW), , ,
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //从20到24的字符,设置文字颜色为黄色
        sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), , ,
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE); //从27到29的字符,设置文字为斜体。
        textView.setText(sp);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
           

2、EditText中的inputType

EditText中的inputType

在编写有EditText的自定义控件的时候可能会用到EditText的inputType属性,直接在xml里

写这个属性的时候是用字符串型的,不过动态设置的时候就变成int型了。

下面是xml文件中EditText里的inputType的所有值和说明。

//

文本类型,多为大写、小写和数字符号。 android:inputType="none" 
android:inputType="text" android:inputType="textCapCharacters" 字母大写 
android:inputType="textCapWords" 首字母大写 android:inputType="textCapSentences" 
仅第一个字母大写 android:inputType="textAutoCorrect" 自动完成 
android:inputType="textAutoComplete" 自动完成
android:inputType="textMultiLine" 多行输入
android:inputType="textImeMultiLine" 输入法多行(如果支持)
android:inputType="textNoSuggestions" 不提示
android:inputType="textUri" 网址
android:inputType="textEmailAddress" 电子邮件地址
android:inputType="textEmailSubject" 邮件主题
android:inputType="textShortMessage" 短讯
android:inputType="textLongMessage" 长信息
android:inputType="textPersonName" 人名
android:inputType="textPostalAddress" 地址
android:inputType="textPassword" 密码
android:inputType="textVisiblePassword" 可见密码
android:inputType="textWebEditText" 作为网页表单的文本
android:inputType="textFilter" 文本筛选过滤
android:inputType="textPhonetic" 拼音输入

//数值类型
android:inputType="number" 数字
android:inputType="numberSigned" 带符号数字格式
android:inputType="numberDecimal" 带小数点的浮点格式
android:inputType="phone" 拨号键盘
android:inputType="datetime" 时间日期
android:inputType="date" 日期键盘
android:inputType="time" 时间键盘
           

代码控制的话, 输入类型的定义主要在如下文件中。

InputType.java (frameworks\base\core\java\android\text)

平时使用时,主要使用其子类EditorInfo:

包名:android.view.inputmethod.EditorInfo

文件:EditorInfo.java (frameworks\base\core\java\android\view\inputmethod)

使用的函数为:TextView.setInputType 。

使用方式举例如下:

setInputType(getInputType() | EditorInfo.TYPE_TEXT_FLAG_AUTO_COMPLETE);