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

来源:互联网 发布:江西先锋软件技术学院 编辑:程序博客网 时间:2024/05/16 07:15

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

      

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

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

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

     布局xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:background="@color/lightgray"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:background="@color/white"        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/ll_mycomment"        >        <TextView            android:id="@+id/tv_cancelcom"            android:textColor="@color/orange"            android:gravity="left"            android:paddingTop="@dimen/padding10"            android:paddingLeft="@dimen/padding10"            android:text="取消"            android:layout_weight="1"            android:textSize="@dimen/text_size_17"            android:layout_width="wrap_content"            android:layout_height="match_parent" />        <LinearLayout            android:layout_weight="1"            android:gravity="center"            android:orientation="vertical"            android:layout_width="wrap_content"            android:layout_height="match_parent">            <TextView                android:textColor="@color/gray"                android:text="发评论"                android:textSize="@dimen/text_size_15"                android:layout_width="wrap_content"                android:layout_height="wrap_content" />            <TextView                android:id="@+id/tv_me"                android:textColor="@color/gray"                android:gravity="center"                android:text="老邓"                android:textSize="@dimen/text_size_12"                android:layout_width="wrap_content"                android:layout_height="wrap_content" />        </LinearLayout>        <TextView            android:id="@+id/tv_sendcom"            android:textColor="@color/lightgray"            android:gravity="right"            android:paddingTop="@dimen/padding10"            android:paddingRight="@dimen/padding10"            android:layout_weight="1"            android:text="发送"            android:textSize="@dimen/text_size_17"            android:layout_width="wrap_content"            android:layout_height="match_parent" />    </LinearLayout>    <EditText        android:id="@+id/et_mycomment"        android:layout_marginTop="@dimen/padding10"        android:textSize="@dimen/text_size_17"        android:gravity="left"        android:hint="写评论..."        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>

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按钮的文字颜色



0 0
原创粉丝点击