Android GUI 之编辑框(EditText)与监视器

下面的例子实现了两个编辑框的事件监听,当编辑相应编辑框时上面的文本框

就会显示当前状态。

第一个编辑框接受拨号键盘的输入,包括数字和一些字符,这些字符有( ) # * / ; - + , . 这10个,

不知道还有没有其他的,第二个接受密码输入,输入的字符将被显示成一个点。




代码:

package com.th;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//获取布局文件中的编辑框
		final EditText ET_phone = (EditText)findViewById(R.id.ET_phonenumber);
		EditText ET_password = (EditText)findViewById(R.id.ET_password);
		//获取布局文件中的文本框
		final TextView text = (TextView)findViewById(R.id.myTextView);
		//为手机号文本框添加监视器
		ET_phone.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				text.setText("Editing ET_phonenumber");		//当 ET_phone 改变时,修改文本框内容
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
			}
		});
		ET_password.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				text.setText("Editing ET_password");	
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}
			
			@Override
			public void afterTextChanged(Editable s) {
			}
		});
	}
}

布局文件:

<!-- main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/myTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="请输入手机号和密码"/>

    <EditText
        android:id="@+id/ET_phonenumber"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:maxLength="40"
        android:hint="Please enter your phone"
        android:textColorHint="#FF000000"
        android:phoneNumber="true">
		<!-- 获得焦点 -->
        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/ET_password"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:maxLength="40"
        android:hint="Please enter your password"
        android:password="true"
        android:textColorHint="#FF000000"/>

</LinearLayout>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值