如果我的字段未使用 textwatcher 填写,如何禁用发送按钮?
How to to disable the send button if my fields are not filled using a textwatcher?
我很难找到解决这种情况的办法。我需要帮助,如果我的字段未使用 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();
}
}
试试这个,并使用 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);
}
}
实际上,您不需要使用 textwatcher 来禁用发送按钮。您所能做的就是在 if 循环中的 onCreate 方法中禁用发送按钮。
Note: Use EditText instead of TextView for taking inputs from user.
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();
}
Again, if there is a necessity of using the textwatcher then do this:
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();
}
希望这对您有所帮助。如有疑问请在评论中回复。
我很难找到解决这种情况的办法。我需要帮助,如果我的字段未使用 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();
}
}
试试这个,并使用 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);
}
}
实际上,您不需要使用 textwatcher 来禁用发送按钮。您所能做的就是在 if 循环中的 onCreate 方法中禁用发送按钮。
Note: Use EditText instead of TextView for taking inputs from user.
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();
}
Again, if there is a necessity of using the textwatcher then do this:
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();
}
希望这对您有所帮助。如有疑问请在评论中回复。