android实战——监听TextView文本Button改变状态

目的:实现微博发状态,当输入框TextView里面的有了内容时,发送按钮(Button)的状态(文字)颜色发生改变。

      android实战——监听TextView文本Button改变状态_第1张图片

        思路:既然按钮随文字内容的变化而改变状态,自然想到要监听EditText的状态,当内容不为空的时候设置按钮的文字颜色。

即监听你在EditText中输入的字数的状态和变化,以便于我们能做相应的提示和操作。

     android为我们提供了一个方法addTextChangedListener实现对输入文本的监控。下边是我自己写的一个Demo。

     布局xml:




    
        

        
            
            
        

        

    

    


java实现:

package yx.communitysocket.com.socket.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import yx.communitysocket.R;

/**
 * Created by deng.jun on 2016/10/9. 15:15
 * 描述:发送评论的界面 可以复用
 */

public class SendCommentActivity extends Activity {

    TextView tv_cancelcom, tv_me, tv_sendcom;
    EditText et_mycomment;
    String mycommment;

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

    public void initView() {
        tv_cancelcom = (TextView) findViewById(R.id.tv_cancelcom);
        tv_me = (TextView) findViewById(R.id.tv_me);
        tv_sendcom = (TextView) findViewById(R.id.tv_sendcom);
        et_mycomment = (EditText) findViewById(R.id.et_mycomment);


        et_mycomment.addTextChangedListener(mTextWatcher);  // 输入文本框监听字数变化

        tv_sendcom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mycommment)) {
                    return;
                } else {

                    //将数据(帖子id,评论人id,评论内容)发给服务器,等待服务器的响应 待写


                    Intent intent = new Intent(); //此处考虑intent是否可以通过别的方式获取,不要新建,节约内存
                    //把返回数据存入Intent
                    intent.putExtra("result", mycommment);
                    //设置返回数据
                    SendCommentActivity.this.setResult(RESULT_OK, intent);
                    //关闭Activity
                    SendCommentActivity.this.finish();
                }
            }
        });

        tv_cancelcom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    TextWatcher mTextWatcher = 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) {
            Log.i("CharSequence------>", s.toString());
            if (s.toString() != "") {
                tv_sendcom.setTextColor(getResources().getColor(R.color.orange));

            }
//              else{
//                  tv_sendcom.setTextColor(getResources().getColor(R.color.blue));
//              }

        }

        @Override
        public void afterTextChanged(Editable s) {
            mycommment = et_mycomment.getText().toString();
            if (TextUtils.isEmpty(mycommment)) {
                tv_sendcom.setTextColor(getResources().getColor(R.color.gray));
            }
            Log.i("变化后的字符------>", mycommment);

        }
    };

}


解析:重写onTextChange()方法,并判断EditText的内容是否为空,当不为空时,设置Button按钮的文字颜色



你可能感兴趣的:(android开发经验)