修改Button、TextView选中变化状态

本文介绍如何在Android应用中自定义Button和TextView的状态样式,包括按钮选中和文本选中状态的颜色变化,并展示了如何使用XML文件定义这些样式以及通过监听器实现更复杂的状态切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、修改Button按钮选中状态

1.在res\values\colors.xml文件中添加如下内容:

<resources> 
   <drawable name="white">#ffffff</drawable>
   <drawable name="black">#000000</drawable>
</resources>

注意:节点一定是drawable,不是color

2. 在res\drawable\下新建一个名为btn_selector.xml的文件,内容如下:

1	<?xml version="1.0" encoding="UTF-8"?>
2	<selector xmlns:android="http://schemas.android.com/apk/res/android">
3	    <item android:state_selected="true" android:color="@drawable/white" />
4	    <item android:state_pressed="true" android:color="@drawable/white" />
5	    <item android:color="@drawable/black"/>
6	</selector>

3.给button设置颜色,内容如下:

 <Button
     android:text="按钮"
     android:textColor="@drawable/btn_selector"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>

这样你的按钮不光背景可以变化,颜色也可以变化

二、修改TextView文本选中状态

1.在res\values\colors.xml文件中添加如下内容:

 <color name="blue">#3f9ddf</color>

2.在res\drawable\下新建一个名为corner_blue_d25.xml的文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:radius="@dimen/d25"/>
    <solid android:color="@color/blue"/>
</shape>

3.在相同目录下新建另一个文件tv_enable_select.xml,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/corners_blue_d25" android:state_pressed="true" />
    <item android:drawable="@drawable/corners_gray_d25" android:state_pressed="false" />
</selector>

 

4.给TextView设置背景,内容如下:

    <TextView
         android:text="文本"
         android:background="@drawable/tv_enable_select"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>

三、还可以通过监听器设置变化状态:

btn.setOnTouchListener(buttonListener);

private OnTouchListener buttonListener = new OnTouchListener() {
 public boolean onTouch(View arg0, MotionEvent event) {
  // TODO Auto-generated method stub
  int action = event.getAction();
  if (action == MotionEvent.ACTION_DOWN) { // 按下
  
   } else if (action == MotionEvent.ACTION_UP) { // 松开   
  
  }
   return false; 
  }  
};

里面写上自己的逻辑即可,大功告成!

package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.Checkable; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.RadioButton; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1 = (Button)findViewById(R.id.b1); t1 = (TextView)findViewById(R.id.t1); t2 = (TextView)findViewById(R.id.t2); t3 = (TextView)findViewById(R.id.t3); t4 = (TextView)findViewById(R.id.t4); c1 = (CheckBox)findViewById(R.id.c1); c2 = (CheckBox)findViewById(R.id.c2); r1 = (RadioButton)findViewById(R.id.r1); r2 = (RadioButton)findViewById(R.id.r2); c1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { t2.setText("C1 已被选中"); } else { t2.setText("C1 未被选中"); } } }); c2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { t3.setText("C2 已被选中"); } else { t3.setText("C2 未被选中"); } } }); r1.setOnCheckedChangeListener((group, checkedId) -> { switch (checkedId) { case R.id.r1: t4.setText("You selected Option 1"); break; 修改
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值