欢迎光临散文网 会员登陆 & 注册

第五章 用户界面基础(EditText)

2018-11-03 21:43 作者:swiss126  | 我要投稿

参考资料:

Android应用程序开发》ISBN 9787302283164

参考软件:

Android Studio、Eclipse+ADT、Android SDK、JDK

用户界面基础(接上节内容)

二、edittext

常用属性

android:layout_gravity="center_vertical"//设置控件显示的位置:默认top,这里居中显示,还有bottom

android:hint="请输入数字!"//设置显示在空间上的提示信息

android:numeric="integer"//设置只能输入整数,如果是小数则是:decimal

android:singleLine="true"//设置单行输入,一旦设置为true,则文字不会自动换行。

android:gray="top" //多行中指针在第一行第一位置et.setSelection(et.length());//调整光标到最后一行

android:autoText //自动拼写帮助

android:capitalize //首字母大写

android:digits //设置只接受某些数字

android:singleLine //是否单行或者多行,回车是离开文本框还是文本框增加新行

android:numeric //只接受数字

android:password //密码

android:phoneNumber // 输入电话号码

android:editable //是否可编辑

android:autoLink=”all” //设置文本超链接样式当点击网址时,跳向该网址

android:password="true"//设置只能输入密码

android:textColor = "#ff8c00"//字体颜色

android:textStyle="bold"//字体,bold, italic, bolditalic

android:textSize="20dip"//大小

android:capitalize = "characters"//以大写字母写

android:textAlign="center"//EditText没有这个属性,但TextView有

android:textColorHighlight="#cccccc"//被选中文字的底色,默认为蓝色

android:textColorHint="#ffff00"//设置提示信息文字的颜色,默认为灰色

android:textScaleX="1.5"//控制字与字之间的间距

android:typeface="monospace"//字型,normal, sans, serif, monospace

android:background="@null"//空间背景,这里没有,指透明

android:layout_weight="1"//权重 在控制控   件显示的大小时蛮有用的。

android:textAppearance="?android:attr/textAppearanceLargeInverse"//文字外观,这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。

常用代码

1、代码示例

 <TableLayout  xmlns:android="http://schemas.android.com/apk/res/android"

     android:layout_width="match_parent"

     android:layout_height="match_parent"

     android:orientation="vertical"

     android:stretchColumns="1" >

    <TableRow  >

         <TextView

             android:id="@+id/name_text"

            android:layout_width="wrap_content"

             android:layout_height="wrap_content"

             android:text="姓名:"/>

         <EditText

             android:id="@+id/name_edit"

             android:layout_height="wrap_content"

             android:layout_width="wrap_content"

             android:selectAllOnFocus="true"

            android:hint="请输入您的姓名"/>

     </TableRow>

    <TableRow  >

         <TextView

             android:id="@+id/code_text"

             android:layout_width="wrap_content"

             android:layout_height="wrap_content"

             android:text="密码:"/>

         <EditText

             android:id="@+id/code_edit"

             android:layout_height="wrap_content"

             android:layout_width="wrap_content"

             android:inputType="numberPassword" />

     </TableRow>

    <TableRow  >

        <TextView  

             android:id="@+id/addr_text"

             android:layout_width="wrap_content"

             android:layout_height="wrap_content"

            android:text="年龄:"/>

         <EditText

             android:id="@+id/addr_edit"

             android:layout_height="wrap_content"

             android:layout_width="wrap_content"

             android:inputType="number" />

     </TableRow>

    <TableRow  >

         <TextView

             android:id="@+id/birth_text"

             android:layout_width="wrap_content"

             android:layout_height="wrap_content"

             android:text="生日:"/>

         <EditText

             android:id="@+id/birth_edit"

             android:layout_height="wrap_content"

             android:layout_width="wrap_content"

             android:inputType="date" />

     </TableRow>

    <TableRow  >

         <TextView

             android:id="@+id/phone_text"

            android:layout_width="wrap_content"

             android:layout_height="wrap_content"

             android:text="TEL:"/>

         <EditText

             android:id="@+id/phone_edit"

             android:layout_height="wrap_content"

             android:layout_width="wrap_content"

             android:selectAllOnFocus="true"

            android:hint="请输入您的电话号码"

             android:inputType="phone" />

     </TableRow>

    <Button

         android:id="@+id/press_ok"

         android:layout_height="wrap_content"

         android:layout_width="wrap_content"

        android:text="确认输入"/>

</TableLayout>

2、添加图片主要用SpannableString和ImageSpan类

Drawable drawable =  getResources().getDrawable(id); 

        drawable.setBounds(0, 0,  drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 

        //需要处理的文本,[smile]是需要被替代的文本 

        SpannableString spannable = new  SpannableString(getText().toString()+"[smile]"); 

        //要让图片替代指定的文字就要用ImageSpan 

        ImageSpan span = new  ImageSpan(drawable, ImageSpan.ALIGN_BASELINE); 

        //开始替换,注意第2和第3个参数表示从哪里开始替换到哪里替换结束(start和end) 

       //最后一个参数类似数学中的集合,[5,12)表示从5到12,包括5但不包括12   

        spannable.setSpan(span,  getText().length(),getText().length()+"[smile]".length(),  Spannable.SPAN_INCLUSIVE_EXCLUSIVE);     

        setText(spannable);

3、将需要的文字高亮显示

public void highlight(int start,int end){

        SpannableStringBuilder spannable=new SpannableStringBuilder(getText().toString());//用于可变字符串

        ForegroundColorSpan span=new ForegroundColorSpan(Color.RED);

        spannable.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        setText(spannable);

    }  

4、加下划线:

public void underline(int start,int end){

        SpannableStringBuilder spannable=new SpannableStringBuilder(getText().toString());

        CharacterStyle span=new UnderlineSpan();

        spannable.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        setText(spannable);

    }

5、长度和空白提示文字,提示文字颜色,是否可编辑等

EditText有一些属性可以设置EditText的特性,比如最大长度,空白提示文字等。

1.    有时候我们有一些特属的需要,要求只能在EditText中输入特定个数的字符,比如身份证号、手机号吗等。这时候就可以通过android:maxLength属性来设置最大输入字符个数,比如android:maxLength=“4”就表示最多能输入4个字符,再多了就输入不进去了。

2.    空白提示文字。有时候我们需要说明你定义的这个EditText是做什么用的,比如让输入用户名,或者输入电话号码等,但是你又不想在EditText前面加一个TextView来说明这是输入用户名的,因为这会使用一个TextView,那么怎么办呢?EditText为我们提供了android:hint来设置当EditText内容为空时显示的文本,这个文本只在EditText为空时显示,你输入字符的时候就消失了,不影响你的EditText的文本。。修改main.xml如下: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:orientation="vertical"  

4.     android:layout_width="fill_parent"  

5.     android:layout_height="fill_parent"  

6.     >  

7. <EditText  

8.     android:id="@+id/edit_text"    

9.     android:layout_width="fill_parent"   

10.    android:layout_height="wrap_content"  

11.    android:maxLength="40"  

12.    android:hint="请输入用户名..."/>  

13.</LinearLayout>  

 运行应用就会看到如下的效果: 


 看看吧,简洁明了还不用新增一个TextView说明,也不影响用户操作。

3.    上面列出了空白时的提示文字,有的人说了,我不想要这个灰色的提示文字,和我的应用整体风格不协调,那也行啊,我们可以换颜色,怎么换呢,就是通过android:textColorHint属性设置你想要的颜色。修改main.xml如下:

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:orientation="vertical"  

4.     android:layout_width="fill_parent"  

5.     android:layout_height="fill_parent"  

6.     >  

7. <EditText  

8.     android:id="@+id/edit_text"    

9.     android:layout_width="fill_parent"   

10.    android:layout_height="wrap_content"  

11.    android:maxLength="40"  

12.    android:hint="请输入用户名..."  

13.    android:textColorHint="#238745"/>  

14.</LinearLayout>  

 运行程序效果如下: 


 看到了吧,颜色已经变了。。

4.    还有一个比较实用的功能,就是设置EditText的不可编辑。设置android:enabled="false"可以实现不可编辑,可以获得焦点。这时候我们看到EditText和一个TextView差不多: 


 

5.    实现类似htmlTextarea的文本域。在Android中没有专门的文本域组件,但是可以通过设置EditText的高来实现同样的文本域功能。修改main.xml如下: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:orientation="vertical"  

4.     android:layout_width="fill_parent"  

5.     android:layout_height="fill_parent"  

6.     >  

7. <EditText  

8.     android:id="@+id/edit_text"    

9.     android:layout_width="fill_parent"   

10.    android:layout_height="200dip"/>  

11.</LinearLayout>  

 运行程序效果如下: 

6、输入特殊格式的字符

在我们开发程序的时候不免会输入一些特属个数的字符,比如密码(输入框的字符要加密显示),电话号码(比如数字和-),数字等,这些都算是一些特属格式的字符,强大的EditText同样为我们提供了输入这些特属格式字符的设置。

1.    密码文本框。密码输入也是Android应用常用的功能,通过配置EditText的android:password="true"就可以实现这一密码输入功能,修改main.xml如下: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:orientation="vertical"  

4.     android:layout_width="fill_parent"  

5.     android:layout_height="fill_parent"  

6.     >  

7. <EditText  

8.     android:id="@+id/edit_text"    

9.     android:layout_width="fill_parent"   

10.    android:layout_height="wrap_content"  

11.    android:password="true"/>  

12.</LinearLayout>  

 运行效果如下: 


 可以看到我们输入的字符已经被“.”这样的掩码所代替。

2.    手机中发短信打电话是必不可少的,所以用于专门输入电话号码的文本框也是大有用途,有了他我们对是否是电话号码的校验就容易的多了(因为字符是正确的,只要校验格式 ).通过设置android:phoneNumber="true"就可以把EditText变成只接受电话号码输入的文本框,连软键盘都已经变成拨号专用软键盘了,所以不用再担心输入其他字符了。修改main.xml如下: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:orientation="vertical"  

4.     android:layout_width="fill_parent"  

5.     android:layout_height="fill_parent"  

6.     >  

7. <EditText  

8.     android:id="@+id/edit_text"    

9.     android:layout_width="fill_parent"   

10.    android:layout_height="wrap_content"  

11.    android:phoneNumber="true"/>  

12.</LinearLayout>  

 运行程序效果如下: 


 注意看软键盘,已经变成拨号专用的啦.

3.    有时候我们只想输入数字,不想输入字母,EditText为我们提供了android:numeric来控制输入的数字类型,一共有三种分别为integer(正整数)、signed(带符号整数)和decimal(浮点数)。这里以signed类型的为例,修改main.xml如下: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:orientation="vertical"  

4.     android:layout_width="fill_parent"  

5.     android:layout_height="fill_parent"  

6.     >  

7. <EditText  

8.     android:id="@+id/edit_text"    

9.     android:layout_width="fill_parent"   

10.    android:layout_height="wrap_content"  

11.    android:numeric="signed"/>  

12.</LinearLayout>  

 运行效果如下: 


 注意这里的软键盘变成“数字键盘”的变化.

7、为文本指定特定的软键盘类型

前面我们通过指定为电话号码特定格式,然后键盘类型变成了拨号专用的键盘,这个是自动变的,其实我们也可以通过android:inputType来设置文本的类型,让输入法选择合适的软键盘的。。android:inputType有很多类型,这里使用date类型来演示,修改main.xml如下: 

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:orientation="vertical"  

4.     android:layout_width="fill_parent"  

5.     android:layout_height="fill_parent"  

6.     >  

7. <EditText  

8.     android:id="@+id/edit_text"    

9.     android:layout_width="fill_parent"   

10.    android:layout_height="wrap_content"  

11.    android:inputType="date"/>  

12.</LinearLayout>  

  运行效果如下: 

 

8、Enter键图标的设置

软键盘的Enter键默认显示的是“完成”文本,我们知道按Enter建表示前置工作已经准备完毕了,要去什么什么啦。比如,在一个搜索中,我们输入要搜索的文本,然后按Enter表示要去搜索了,但是默认的Enter键显示的是“完成”文本,看着不太合适,不符合搜索的语义,如果能显示“搜索”两个字或者显示一个表示搜索的图标多好。事实证明我们的想法是合理的,Android也为我们提供的这样的功能。通过设置android:imeOptions来改变默认的“完成”文本。这里举几个常用的常量值:

1.    actionUnspecified 未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.效果:

2.    actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE 效果:

3.    actionGo 去往,对应常量EditorInfo.IME_ACTION_GO 效果:

4.    actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH 效果: 

5.    actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND 效果:

6.    actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT 效果:

7.    actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE 效果:

 下面已搜索为例,演示一个实例,修改main.xml如下:

Xml代码  

1. <?xml version="1.0" encoding="utf-8"?>  

2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.     android:orientation="vertical"  

4.     android:layout_width="fill_parent"  

5.     android:layout_height="fill_parent"  

6.     >  

7. <EditText  

8.     android:id="@+id/edit_text"    

9.     android:layout_width="fill_parent"   

10.    android:layout_height="wrap_content"  

11.    android:imeOptions="actionSearch"/>  

12.</LinearLayout>  

  修改HelloEditText如下:

Java代码  

1. package com.flysnow;  

2.   

3. import android.app.Activity;  

4. import android.os.Bundle;  

5. import android.view.KeyEvent;  

6. import android.widget.EditText;  

7. import android.widget.TextView;  

8. import android.widget.Toast;  

9. import android.widget.TextView.OnEditorActionListener;  

10.  

11.public class HelloEditText extends Activity {  

12.    /** Called when the activity is first created. */  

13.    @Override  

14.    public void onCreate(Bundle savedInstanceState) {  

15.        super.onCreate(savedInstanceState);  

16.        setContentView(R.layout.main);  

17.        EditText editText=(EditText)findViewById(R.id.edit_text);  

18.        editText.setOnEditorActionListener(new OnEditorActionListener() {  

19.            @Override  

20.            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  

21.                Toast.makeText(HelloEditText.this, String.valueOf(actionId), Toast.LENGTH_SHORT).show();  

22.                return false;  

23.            }  

24.        });  

25.    }  

26.}  

 运行程序,点击回车(也就是搜索图标软键盘按钮)会显示该actionId.我们上面的每一个设置都会对应一个常量,这里的actionId就是那个常量值。 


第五章 用户界面基础(EditText)的评论 (共 条)

分享到微博请遵守国家法律