我想创建一个必须具有edittexts和两个按钮ok和cancel的活动,其中ok按钮应该首先被禁用,并且应该在验证用户名和密码时启用。 我的代码是 -
public class DisableButton extends Activity {
EditText et1,et2;
Button b1,b2;
String et_value, pwd_value;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_disable_button);
et1=(EditText)findViewById(R.id.editText1);
et2=(EditText)findViewById(R.id.editText2);
b1=(Button)findViewById(R.id.button1);
b1.setEnabled(false);
b2=(Button)findViewById(R.id.button2);
et_value=et1.getText().toString();
pwd_value=et2.getText().toString();
if((et_value=="pbpathi")&&(pwd_value=="1234"))
{
b1.setEnabled(true);
}
}
public void okClick(View v){
Toast.makeText(getBaseContext(), "Username is "+et_value+" \n password is
"+pwd_value, Toast.LENGTH_LONG).show();
}
public void cancelClick(View v){
}
}
我的xml文件是 -
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
tools:context="com.example.labs.DisableButton" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="OK"
android:onClick="okClick"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"
android:onClick="cancelClick" />
</LinearLayout>
答案 0 :(得分:2)
首先,
你不应该获取edittexts&#39; onCreate
中的值,但onClick
其次,
您不应该使用==
来比较字符串。它会比较对象。
因此请将代码更改为:
public void okClick(View v){
Toast.makeText(getBaseContext(), "Username is "+et_value+" \n password is
"+pwd_value, Toast.LENGTH_LONG).show();
et_value=et1.getText().toString();
pwd_value=et2.getText().toString();
// You can use equalsIgnoreCase for cas insensitive comparison
if((et_value.equals("pbpathi"))&&(pwd_value.equals("1234")))
{
b1.setEnabled(true);
}
}
希望得到这个帮助。
答案 1 :(得分:0)
if((et_value.equals("pbpathi")&&(pwd_value.equals("1234")))
{
b1.setEnabled(true);
}
答案 2 :(得分:0)
使用equals
代替==
。
if("pbpathi".equals(et_value) && "1234".equals(pwd_value)) {
....
}
答案 3 :(得分:0)
public class DisableButton extends Activity {
EditText et1, et2;
Button b1, b2;
String et_value, pwd_value;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
b1 = (Button) findViewById(R.id.button1);
b1.setEnabled(false);`enter code here`
b2 = (Button) findViewById(R.id.button2);
et1.addTextChangedListener(mTextWatcher);
et2.addTextChangedListener(mTextWatcher);
}
public void okClick(View v) {
et_value = et1.getText().toString();
pwd_value = et2.getText().toString();
Toast.makeText(getBaseContext(),
"Username is " + et_value + " \n password is" + pwd_value,
Toast.LENGTH_LONG).show();
}
public void cancelClick(View v) {
}
private final TextWatcher mTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void afterTextChanged(Editable s) {
if ((et1.getText().toString().equals("pbpathi"))
&& (et2.getText().toString().equals("1234"))) {
b1.setEnabled(true);
}
}
};
}