我正在进行具有用户代码和密码的登录。然后,当字段完成后,会出现一个按钮"登录"那是set.Enabled(false),它将被设置。允许(允许访问)(我这样做是为了避免测试一致性)。
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText codigo;
final TextView nome;
final EditText senha;
codigo = (EditText) findViewById(R.id.cod_edit_text);
senha = (EditText)findViewById(R.id.senha_edit_text);
String cod = codigo.getEditableText().toString();
String password = senha.getEditableText().toString();
final Button botaologar = (Button) findViewById(R.id.button_logar);
if(!cod.isEmpty() || !password.isEmpty())
{
botaologar.setClickable(true);
botaologar.setEnabled(true);
}
else
{
botaologar.setEnabled(false);
botaologar.setClickable(false);
}
但是,当我捕获EditText的字符串时,由于某种原因它是空的,并且按钮仍然设置。启用(false)。
我注意到,当我在activity.main.xml的XML上使用带有某个值的string.XML上的字符串命名时,该按钮保持set.Enabled(true),但该值显示在EditText上,其值为string.xml。看:
文件activity_main.xml:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:text="@string/cod_edit_text"
android:id="@+id/cod_edit_text"
android:layout_below="@+id/codigo_text_view"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/senha_edit_text"
android:text="@string/senha_edit_text"
style="@style/AppTheme"
android:password="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/senha_text_view"
android:layout_alignParentLeft="true" />
文件string.xml:
<string name="cod_edit_text"> test </string>
<string name="senha_edit_text"> test </string>
所以,问题是我无法按下按钮.setEnabled(true)因为我无法正确获取EditText的值。有什么想法来解决这个问题吗?
答案 0 :(得分:2)
使用 TextWatcher :
示例强>
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText passwordEditText;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Initializing views */
passwordEditText = (EditText) findViewById(R.id.password);
textView = (TextView) findViewById(R.id.passwordHint);
textView.setVisibility(View.GONE);
/* Set Text Watcher listener */
passwordEditText.addTextChangedListener(passwordWatcher);
}
private final TextWatcher passwordWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
textView.setVisibility(View.VISIBLE);
}
public void afterTextChanged(Editable s) {
if (s.length() == 0) {
textView.setVisibility(View.GONE);
} else{
textView.setText("You have entered : " + passwordEditText.getText());
}
}
};
}
您始终可以在TextWatcher的beforeTextChanged或afterTextChanged中启用或禁用该按钮。根据您的要求。
答案 1 :(得分:0)
也许有更好的方法,但是只要有变化和/或字符串不为空,你肯定可以使用一个或两个TextWatcher进行更新:
codigo.addTextChangedListener(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) {
cod = s.toString();
if(!cod.isEmpty() || !password.isEmpty())
{
botaologar.setClickable(true);
botaologar.setEnabled(true);
}
else
{
botaologar.setEnabled(false);
botaologar.setClickable(false);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
密码的editText也是如此。