如果 edittext 中缺少文本,则按钮不会禁用
Button not disabling if text missing in edittext
谁能告诉我为什么当 edittext 中的文本为空时我的按钮没有被禁用?我已经尝试过很多方法来做到这一点,但它从来没有奏效!这是我目前的简化代码。
代码:
public class MapStartActivity extends FragmentActivity {
EditText mapName;
Button NextPageStart;
private TextWatcher textWatcher = 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) {
checkFieldsForEmptyValues();
}
@Override
public void afterTextChanged(Editable s) {
}
};
private void checkFieldsForEmptyValues(){
String s1 = mapName.getText().toString();
if (s1.trim().isEmpty()) {
NextPageStart.setEnabled(true);
} else {
NextPageStart.setEnabled(false);
NextPageStart.setAlpha(.5f);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_start);
NextPageStart = (Button) findViewById(R.id.NextStatLocBut);
mapName = (EditText) findViewById(R.id.MapNameText);
//Click Listener for button
NextPageStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mapName.addTextChangedListener(textWatcher);
}
});
}
}
像这样在点击监听器外添加文本观察器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_start);
NextPageStart = (Button) findViewById(R.id.NextStatLocBut);
mapName = (EditText) findViewById(R.id.MapNameText);
// To disable the button intially
NextPageStart.setEnabled(false);
mapName.addTextChangedListener(textWatcher);
//Click Listener for button
NextPageStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// You can do some click action here
}
});
}
我不知道为什么它不适合你,但我想它应该可以正常工作。但你应该先了解名称约定。
在您的代码中仅更改以下行
@Override
public void afterTextChanged(Editable s) {
String textFromEditText = mapName.getText();
if(TextUtils.isEmpty(textFromEditText)){
NextPageStart.setEnabled(false);
} else{
NextPageStart.setEnabled(true);
}
}
谁能告诉我为什么当 edittext 中的文本为空时我的按钮没有被禁用?我已经尝试过很多方法来做到这一点,但它从来没有奏效!这是我目前的简化代码。
代码:
public class MapStartActivity extends FragmentActivity {
EditText mapName;
Button NextPageStart;
private TextWatcher textWatcher = 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) {
checkFieldsForEmptyValues();
}
@Override
public void afterTextChanged(Editable s) {
}
};
private void checkFieldsForEmptyValues(){
String s1 = mapName.getText().toString();
if (s1.trim().isEmpty()) {
NextPageStart.setEnabled(true);
} else {
NextPageStart.setEnabled(false);
NextPageStart.setAlpha(.5f);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_start);
NextPageStart = (Button) findViewById(R.id.NextStatLocBut);
mapName = (EditText) findViewById(R.id.MapNameText);
//Click Listener for button
NextPageStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mapName.addTextChangedListener(textWatcher);
}
});
}
}
像这样在点击监听器外添加文本观察器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_start);
NextPageStart = (Button) findViewById(R.id.NextStatLocBut);
mapName = (EditText) findViewById(R.id.MapNameText);
// To disable the button intially
NextPageStart.setEnabled(false);
mapName.addTextChangedListener(textWatcher);
//Click Listener for button
NextPageStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// You can do some click action here
}
});
}
我不知道为什么它不适合你,但我想它应该可以正常工作。但你应该先了解名称约定。
在您的代码中仅更改以下行
@Override
public void afterTextChanged(Editable s) {
String textFromEditText = mapName.getText();
if(TextUtils.isEmpty(textFromEditText)){
NextPageStart.setEnabled(false);
} else{
NextPageStart.setEnabled(true);
}
}