如果EditText
为空,则登录Button
已被禁用。 如果EditText
有一些文本,然后登录Button
已被启用。 那么你可以看到Instagram的登录此方法。
这两个字段为空,登录Button
被禁用。
这里密码字段为空,所以还是请登录Button
被禁用。
这里既有用户名和密码字段不为空,所以登录Button
被启用。
如何实现这些步骤? 这里是我的代码,它不工作..
EditText et1,et2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_check);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
Button b = (Button) findViewById(R.id.button1);
String s1 = et1.getText().toString();
String s2 = et2.getText().toString();
if(s1.equals("")|| s2.equals("")){
b.setEnabled(false);
} else {
b.setEnabled(true);
}
}
Answer 1:
继承人你在找什么:
private EditText et1,et2;
// create a textWatcher member
private TextWatcher mTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void afterTextChanged(Editable editable) {
// check Fields For Empty Values
checkFieldsForEmptyValues();
}
};
void checkFieldsForEmptyValues(){
Button b = (Button) findViewById(R.id.button1);
String s1 = et1.getText().toString();
String s2 = et2.getText().toString();
if(s1.equals("")|| s2.equals("")){
b.setEnabled(false);
} else {
b.setEnabled(true);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_check);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
// set listeners
et1.addTextChangedListener(mTextWatcher);
et2.addTextChangedListener(mTextWatcher);
// run once to disable if empty
checkFieldsForEmptyValues();
}
Answer 2:
您需要实现TextWatcher上EditText
达到的效果。
EditText et1, et2;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
b = (Button) findViewById(R.id.button1);
checkValidation();
et1.addTextChangedListener(mWatcher);
et2.addTextChangedListener(mWatcher);
}
private void checkValidation() {
// TODO Auto-generated method stub
if ((TextUtils.isEmpty(et1.getText()))
|| (TextUtils.isEmpty(et2.getText())))
b.setEnabled(false);
else
b.setEnabled(true);
}
TextWatcher mWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
checkValidation();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
Answer 3:
你需要跟踪用户的行为里面EditText
使用TextWatcher
对象:
myEditText.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void afterTextChanged(Editable s)
{
if (s.length() > 1)
{
//enable button
} else
//disable
}
});
Answer 4:
试试这个:
EditText et1,et2;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_check);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
b = (Button) findViewById(R.id.button1);
et1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String s1 = et1.getText().toString();
String s2 = et2.getText().toString();
if(s1.equals("") && s2.equals("")){
b.setEnabled(false);
}
else if(!s1.equals("")&&s2.equals("")){
b.setEnabled(false);
}
else if(!s2.equals("")&&s1.equals(""){
b.setEnabled(false);
}
else {
b.setEnabled(true);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
et2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String s1 = et1.getText().toString();
String s2 = et2.getText().toString();
if(s1.equals("") && s2.equals("")){
b.setEnabled(false);
}
else if(!s1.equals("")&&s2.equals("")){
b.setEnabled(false);
}
else if(!s2.equals("")&&s1.equals(""){
b.setEnabled(false);
}
else {
b.setEnabled(true);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
Answer 5:
您需要附上每当在EditText上的一个字段的文本更改被称为TextWatcher。
private EditText mName;
private EditText mPassword;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_check);
mName = (EditText) findViewById(R.id.editText1);
mPassword = (EditText) findViewById(R.id.editText2);
mButton = (Button) findViewById(R.id.button1);
mName.addTextChangedListener(mWatcher);
mPassword.addTextChangedListener(mWatcher);
}
private TextWatcher mWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
boolean nameNotEmpty = mName.getText().length()>0;
boolean pwNotEmpty = mPassword.getText().length()>0;
mButton.setEnabled(nameNotEmpty && pwNotEmpty);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
};
Answer 6:
private TextWatcher mPhoneNumberEtWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.length() >= 10) {
mPhoneImg.setImageDrawable(getResources().getDrawable(R.drawable.phone_activate));
if (mPasswordEt.getText().toString().length() >= 5) {
mLoginBtn.setEnabled(true);
}
} else {
mPhoneImg.setImageDrawable(getResources().getDrawable(R.drawable.phone));
mLoginBtn.setEnabled(false);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
};
mPhoneNumberEt.addTextChangedListener(mPhoneNumberEtWatcher);
您应该使用TextWatcher。 这将用户输入后调用方法。 你可以检查长度和财产以后否则你编辑的文本。
Answer 7:
试试这个
if(s1.equals("") && s2.equals(""))
{
b.setEnabled(true);
// to change color of the button you need to apply style to the button[here refer custom bg][1]
}
else
{
b.setEnabled(false);
//do nothing or display toast msg
}
Answer 8:
我只想补充一点,如果该检查将不起作用 InputType
的的EditText
是一个密码(或类似)和机能的研究,以证明文本的长度(见其他答案)是由被称为
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {...}
因此,它调用从检查是非常重要的
@Override
public void afterTextChanged(Editable s) {...}
Answer 9:
哎,如果想使用代码切断按钮需要改变颜色,如果editText1_id和editText1_passcode是在4位
checkValidation();
editText1_id.addTextChangedListener(mWatcher);
editText1_passcode.addTextChangedListener(mWatcher);
}
private void checkValidation() {
// TODO Auto-generated method stub
if ((TextUtils.isEmpty(editText1_id.getText()))
|| (TextUtils.isEmpty(editText1_passcode.getText())))
loginbtn.setEnabled(false);
else
loginbtn.setEnabled(true);
}
TextWatcher mWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
checkValidation();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
}
Answer 10:
1号线的解决方案?
设置按钮依赖的EditText是超级简单 的数据绑定 。 您可以通过只XML管理。
android:enabled="@{etName.text.length() > 5 && etPassword.text.length() > 5}"
完整的XML -
<LinearLayout
>
<EditText
android:id="@+id/etName"
/>
<EditText
android:id="@+id/etPassword"
/>
<Button
android:enabled="@{etName.text.length() > 5 && etPassword.text.length() > 5}"
/>
</LinearLayout>
文章来源: In Android, how to make Login button disable with respect to EditText?