现在的位置: 首页 > 综合 > 正文

Android用户界面之EditText

2018年02月05日 ⁄ 综合 ⁄ 共 1391字 ⁄ 字号 评论关闭

     和TextView 一样,EditText也是android设计用户界面经常接触到的一个控件.EditText的经常使用到的属性有:

   

属性 意义
android:hint 编辑框为空时,显示的字符
android:textColorHint 编辑框为空是,显示的字符的颜色
android:inputType 限制输入的内容的类型
android:digits 限制输入的内容,可以只取指定的字符
android:maxLenght 限制最长的字符数
android:password 输入密码模式

请看代码:

edit.xml

<EditText 
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/userName"
        android:textColorHint="#898767"/>
    <EditText 
        android:id="@+id/et2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/password"
        android:layout_below="@id/et"
        android:textColorHint="#898767"/>
    <TextView 
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/et2"/>

TestEditText.java

public class TestEditText extends Activity{
	private EditText et,et2;
	private TextView text1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit);
		
		et=(EditText) this.findViewById(R.id.et);
		et2=(EditText) this.findViewById(R.id.et2);
		text1=(TextView) this.findViewById(R.id.text1);
		
		et.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if(keyCode==KeyEvent.KEYCODE_ENTER){
					text1.setText(et.getText().toString());
				}
				
				return false;
			}
		});
		
	}
}

当按Enter键时,TextView控件会显示et这个EditText上的输入的内容

抱歉!评论已关闭.