怎样实现同时监听多个EditText
1个回答
推荐于2017-10-10 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517195
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
/*
*要求一个ACTIVITY中的多个edittext都填写有内容才能按Button进入下一步的实现
*/
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences.Editor;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText etONE;
private EditText etTWO;
private EditText etTHREE;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etONE = (EditText)findViewById(R.id.etOne);
etTWO = (EditText)findViewById(R.id.etTwo);
etTHREE = (EditText)findViewById(R.id.etThree);
btn = (Button)findViewById(R.id.btn);
//为EditText绑定监听器
textChange tc1 = new textChange();
etONE.addTextChangedListener(tc1);
etTWO.addTextChangedListener(tc1);
etTHREE.addTextChangedListener(tc1);
}
//EditText监听器
class textChange implements TextWatcher {
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void onTextChanged(CharSequence cs, int start, int before,
int count) {
boolean Sign1 = etONE.getText().length() > 0;
boolean Sign2 = etTWO.getText().length() > 0;
boolean Sign3 = etTHREE.getText().length() > 0;
if (Sign1&Sign2&Sign3) {
btn.setText("BINGO");
btn.setEnabled(true);
}
//在layout文件中,对Button的text属性应预先设置默认值,否则刚打开程序的时候Button是无显示的
else {
btn.setText("请填写好内容");
btn.setEnabled(false);
}
}
}
}
*要求一个ACTIVITY中的多个edittext都填写有内容才能按Button进入下一步的实现
*/
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences.Editor;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText etONE;
private EditText etTWO;
private EditText etTHREE;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etONE = (EditText)findViewById(R.id.etOne);
etTWO = (EditText)findViewById(R.id.etTwo);
etTHREE = (EditText)findViewById(R.id.etThree);
btn = (Button)findViewById(R.id.btn);
//为EditText绑定监听器
textChange tc1 = new textChange();
etONE.addTextChangedListener(tc1);
etTWO.addTextChangedListener(tc1);
etTHREE.addTextChangedListener(tc1);
}
//EditText监听器
class textChange implements TextWatcher {
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void onTextChanged(CharSequence cs, int start, int before,
int count) {
boolean Sign1 = etONE.getText().length() > 0;
boolean Sign2 = etTWO.getText().length() > 0;
boolean Sign3 = etTHREE.getText().length() > 0;
if (Sign1&Sign2&Sign3) {
btn.setText("BINGO");
btn.setEnabled(true);
}
//在layout文件中,对Button的text属性应预先设置默认值,否则刚打开程序的时候Button是无显示的
else {
btn.setText("请填写好内容");
btn.setEnabled(false);
}
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询