有人可以告诉我如何禁用Android中的按钮,除非所有三个editText字段都填满了吗?我不知道在这里包含条件语句的位置以及如何将它放在按钮监听器的上下文中。
这是我的主要活动代码。
package com.amritayalur.mypowerschool;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MyPowerSchoolActivity extends Activity {
Button buttonSubmit;
TextView textViewTitle;
TextView textViewDesc;
EditText editTextURL, editTextUser, editTextPass;
String url = "";
String str;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonSubmit = (Button) findViewById(R.id.buttonSubmit);
textViewTitle = (TextView) findViewById(R.id.textViewTitle);
textViewDesc = (TextView) findViewById(R.id.textViewDesc);
editTextURL = (EditText) findViewById(R.id.editTextURL);
editTextUser = (EditText) findViewById(R.id.editTextUser);
editTextPass = (EditText) findViewById(R.id.editTextPass);
//Start TextView
textViewTitle.setText("MyPowerSchool");
//button listener
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent( MyPowerSchoolActivity.this, creds.class);
i.putExtra("pschoolurl", editTextURL.getText().toString());
i.putExtra("pschooluser", editTextUser.getText().toString());
i.putExtra("pschoolpass", editTextPass.getText().toString());
// get the text here
final int result = 1;
startActivityForResult(i, result);
};
});
}
}
答案 0 :(得分:11)
您可以使用TextChangeListener
EditText ed = //your EditText
ed.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//Check if 's' is empty
}
@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
}
});
答案 1 :(得分:3)
非常简单,只需使用以下代码
更新onClick()方法即可 @Override
public void onClick(View v)
{
if ( ( !editTextURL.getText().toString().equals("")) && ( !editTextUser.getText().toString().equals("")) && ( !editTextPass.getText().toString().equals("") ) )
{
// TODO Auto-generated method stub
Intent i = new Intent( MyPowerSchoolActivity.this, creds.class);
i.putExtra("pschoolurl", editTextURL.getText().toString());
i.putExtra("pschooluser", editTextUser.getText().toString());
i.putExtra("pschoolpass", editTextPass.getText().toString());
// get the text here
final int result = 1;
startActivityForResult(i, result);
}
};
Above if条件不会禁用您的Button,但仅当您的All EditText不为空时才会允许。
答案 2 :(得分:1)
怎么样:
//button listener
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!"".equals(editTextURL.getText().toString())
&& !"".equals(editTextUser.getText().toString())
&& !"".equals(editTextPass.getText().toString())){
Intent i = new Intent( MyPowerSchoolActivity.this, creds.class);
i.putExtra("pschoolurl", editTextURL.getText().toString());
i.putExtra("pschooluser", editTextUser.getText().toString());
i.putExtra("pschoolpass", editTextPass.getText().toString());
// get the text here
final int result = 1;
startActivityForResult(i, result);
}
};
});
如果你想禁用按钮:
buttonSubmit.setEnabled(false); //disable button;