我很难找到解决这种情况的方法。我需要帮助,如果我的字段没有使用textwatcher填充,我试图禁用发送按钮。以下是代码的一部分:
public class Main5Activity extends AppCompatActivity {
TextView shoppinglist, fullname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
fullname = (TextView) findViewById(R.id.fullname);
shoppinglist = (TextView) findViewById(R.id.shoppinglist);
public void send(View v) {
new Send().execute();
}
}
答案 0 :(得分:0)
试试这个,并使用EditText,TextView不可输入:
editText1.addTextChangedListener(this);
imageButton.setEnable(false);
@Override
public void afterTextChanged(Editable s) {
if (s.toString().equals("")) {
imageButton.setEnabled(false);
} else {
imageButton.setEnabled(true);
}
}
答案 1 :(得分:0)
实际上,您不需要使用textwatcher来禁用发送按钮。您所能做的就是在if循环中禁用onCreate方法中的发送按钮。
注意:使用EditText而不是TextView来获取用户的输入。
public class Main5Activity extends AppCompatActivity {
EditText shoppinglist, fullname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
fullname = findViewById(R.id.fullname);
button1 = (Button) findViewById(R.id.buttsave);
shoppinglist = findViewById(R.id.shoppinglist);
final String username = fullname.getText().toString();
final String shopList = shoppinglist.getText().toString();
if (TextUtils.isEmpty(username)&&TextUtils.isEmpty(shopList)) {
fullname.setError("Please enter your fullname");
shoppinglist.setError("Please select shopping list");
button1.setEnabled(false);
}
else {
button1.setEnabled(true);
}
//close onCreate here
}
public void send(View v) {
// it is not possible to enable the send button here because you are already
// executing the send method. So it should have already been enabled.
new Send().execute();
}
同样,如果有必要使用textwatcher,请执行以下操作:
public class Main5Activity extends AppCompatActivity implements TextWatcher {
EditText shoppinglist, fullname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
fullname = findViewById(R.id.fullname);
button1 = (Button) findViewById(R.id.buttsave);
shoppinglist = findViewById(R.id.shoppinglist);
fullname.addTextChangedListener(this);
shoppinglist.addTextChangedListener(this);
}
@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) {
final String username = fullname.getText().toString();
final String shopList = shoppinglist.getText().toString();
if (TextUtils.isEmpty(username)&&TextUtils.isEmpty(shopList)) {
fullname.setError("Please enter your fullname");
shoppinglist.setError("Please select shopping list");
button1.setEnabled(false);
}
else {
button1.setEnabled(true);
}
}
public void send(View v) {
button1.setEnabled(true);
new Send().execute();
}
希望这会有所帮助。如果您仍有任何疑问,请在评论中回复。