当EditText为空时更改按钮颜色 - android

时间:2014-02-27 04:01:39

标签: android button colors android-edittext

我想知道当特定的edittext为空或不为空时是否可以更改按钮的颜色。就像edittext为空时,按钮显示“ADD”,当它不为空时,它会显示“CHANGE”。有人能帮我吗?提前谢谢。

3 个答案:

答案 0 :(得分:4)

关于Button Text设置如:

if(editText.getText().toString.length()>0){
yourbutton.setText("Change");
}else{
yourbutton.setText("Add");
}

如果您实施EditText addTextChangedListener,那么

 editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence str, int start, int count, int after)    {

        }

        @Override
        public void onTextChanged(CharSequence str, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable str) {
             if(str.toString().trim().length()>0){
                 yourbutton.setText("Change");
             }else{
                 yourbutton.setText("Add");
             }
        }
    });

您还可以动态设置Button Text Color

  yourbutton.setTextColor(Color.BLUE);

如需更多信息,请访问:http://developer.android.com/reference/android/content/res/ColorStateList.html

答案 1 :(得分:2)

// try this way 

**activity_main.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/edtValue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btnAddOrChange"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="Add"/>
</LinearLayout>


**MyActivity.java**
 public class MyActivity extends Activity{

    private EditText edtvalue;
    private Button btnAddOrChange;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edtvalue = (EditText) findViewById(R.id.edtValue);
        btnAddOrChange = (Button) findViewById(R.id.btnAddOrChange);

        edtvalue.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(s.toString().trim().length()>0){
                     btnAddOrChange.setText("Change");
                 }else{
                     btnAddOrChange.setText("Add");
                 }
            }
        });
    }

}

答案 2 :(得分:2)

更改Button's文字,background color执行此类操作

urEditText.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(s.toString().trim().length()>0){
                     urBtn.setBackgroundColor(Color.GREY);
                     urBtn.setText("Change");
                 }else{
                     urBtn.setBackgroundColor(Color.WHITE);
                     urBtn.setText("Add");
                 }
            }
        });