【问题标题】:Android:Disable Button on app launch enable only when Edit text is filledAndroid:仅在填充编辑文本时启用应用启动时禁用按钮
【发布时间】:2021-02-12 09:47:30
【问题描述】:

我想 Android:Disable Button on app launch 只有在编辑文本被填充时才启用。需要帮助。提前致谢。我是安卓初学者。 尝试使用 TextWatcher。当我不添加此功能时,应用程序运行良好。 请建议我如何实现 TextWatcher 部分。应用程序的其余部分工作正常。

代码如下:

package com.example.intentopennextscreen;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

  public TextView editText1;
  public Button submit;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



      //editText1.addTextChangedListener(ebert);
    }


    

  /*TextWatcher ebert = 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) {
      String message = editText1.getText().toString().trim();

      submit.setEnabled(!message.isEmpty());
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
  };  */




    /* Called when the user taps the Send button */
    public void sendMessage(View view) {

      /* Creating instance of Intent object */
      Intent intent = new Intent(this, DisplayMessageActivity.class);

      /* finding the user-input provided in the editText as an object  */
      EditText editText1 = (EditText) findViewById(R.id.editText1);

      /*Finding Button Element*/
      Button submit = (Button) findViewById(R.id.button);













      /* converting the user-input of editText as String  */
      String message = editText1.getText().toString().trim();







        /* sending the message to other activity as Key-Value Pair */
        intent.putExtra("Value", message);

        /* starting the intent */
        startActivity(intent);
    }
}

【问题讨论】:

    标签: android android-edittext android-button textwatcher android-textwatcher


    【解决方案1】:

    你可以在afterTextChanged里面移动按钮操作

    @Override
    public void afterTextChanged(Editable s) {
        submit.setEnabled(!s.isEmpty());
    }
    

    【讨论】:

      【解决方案2】:

      试试下面的代码,
      将此添加到您的 onCreate() 方法中

          submit.setEnabled(false);
          editText1.addTextChangedListener(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) {
      
              }
      
              @Override
              public void afterTextChanged(Editable s) {
                  if (editText1.getText().length() > 0) {
                      submit.setEnabled(true);
                  } else {
                      submit.setEnabled(false);
                  }
              }
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-12
        相关资源
        最近更新 更多