如何锁定 android 中的按钮并在特定输入后解锁?

How to lock a button in android and unlock it after a certain input?

例如,我制作了一个按钮,可以将我带到另一个 activity,我可以在那里输入问题,我想锁定该按钮,使其无法访问。但在您输入某个单词或类似密码后,该按钮将解锁。有谁知道如何实现这个?请帮忙谢谢。

您可以启用和禁用任何视图,包括按钮

Button btn = findViewById(R.id.button);
makeInteractable(btn,false); // disable button first

private void makeInteractable(View v, boolean interactable){
   view.setEnabled(interactable);
}

您还可以将 TextChangedListener 添加到 EditText 并更新按钮以使其在输入某些数据后变得难以处理

textfield.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable s) {}

   @Override    
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   @Override    
   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      if(conditionMeet())
        makeInteractable(btn, true);
   }
  });

启用或禁用按钮的最佳且简单的方法是使用 .setClickable() 函数。

btn_next.setClickable(true); //to enable the button

btn_next.setClickable(false); // to disable the button