Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

How to disable the button if Edittext is empty and pass the Edittext input to another activity in android studio?

I am creating a game and the players need to put their name into the EditText and pass it to another screen, but the button must be disabled while the EditText is empty. And it will become enabled if the players enter their name and when they press Enter, it will proceed to another screen welcoming the player.

question from:https://stackoverflow.com/questions/66058430/how-to-disable-the-button-if-edittext-is-empty-and-pass-the-edittext-input-to-an

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can add text changed listener to your edit text and if user entered the name enable the button.

yourEditText.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {
     // enable your button here.
   }

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

   public void onTextChanged(CharSequence s, int start, int before, int count)
   {}
});

For passing this value into another activity you can add bundle to your intent:

  Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
  Bundle b = new Bundle();
  b.putString("playerName", name); // Player name
  intent.putExtras(b);
  startActivity(intent);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...