简体   繁体   English

按下时更改按钮文本颜色

[英]change button text color when pressed

I have made my button transparent so I would like to have the button text color change when the button is pressed.我已使按钮透明,因此我希望在按下按钮时更改按钮文本颜色。 Is it possible to do this using just xml files?是否可以仅使用 xml 文件来执行此操作?

Yes, you can do it like that:是的,你可以这样做:

layout/main_layout.xml:布局/main_layout.xml:

.....
    <Button
      android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="bonjour !"
      android:textColor="@color/button_text_color"
    />
.....

color/button_text_color.xml:颜色/button_text_color.xml:

   <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:color="#c0c0c0" android:state_pressed="true"/>
     <item android:color="#ffffff"/>
   </selector>

See the section called State List in this bit of documentation... Drawable Resources .请参阅此文档中名为State List的部分... Drawable Resources

You can define two different Button xml files one for the transparent 'default' state and another with the button as Red for your 'pressed' state.您可以定义两个不同的Button xml 文件,一个用于透明的“默认”状态,另一个将按钮定义为红色,用于“按下”状态。 You then define a selector which switches the drawable resources in the different states.然后定义一个selector ,用于在不同状态下切换可绘制资源。

EDIT: As per devunwired's comment the Color State List resource is probably more suitable for just changing colours rather than the drawable itself.编辑:根据 devunwired 的评论,颜色状态列表资源可能更适合仅更改颜色而不是可绘制对象本身。

I like the solution proposed by Konstantin Burov in the other issue: Android customized button;喜欢Konstantin Burov在另一期提出的解决方案: Android自定义按钮; changing text color 改变文字颜色

You can actually manage more states than just pressed and normal.您实际上可以管理更多状态,而不仅仅是按下和正常。 But it should solve the problem.但它应该可以解决问题。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Focused and not pressed -->
<item android:state_focused="true" 
      android:state_pressed="false" 
      android:color="#ffffff" />

<!-- Focused and pressed -->
<item android:state_focused="true" 
      android:state_pressed="true" 
      android:color="#000000" />

<!-- Unfocused and pressed -->
<item android:state_focused="false" 
      android:state_pressed="true" 
      android:color="#000000" />

<!-- Default color -->
<item android:color="#ffffff" />
</selector>

Then you can use that selector drawable in your button changing the text color attribute like below.然后您可以在按钮中使用该选择器 drawable 更改文本颜色属性,如下所示。 Note that the selector in the example below is named "button_text_color"请注意,下面示例中的选择器名为“button_text_color”

android:textColor="@drawable/button_text_color" android:textColor="@drawable/button_text_color"

Using the same drawable approach you can also solve the background color of the button.使用相同的可绘制方法,您还可以解决按钮的背景颜色。 Just remember that in the selector instead of using the "android:color" attribute you need to use the "android:drawable" attribute like below.请记住,在选择器中,您需要使用“android:drawable”属性,而不是使用“android:color”属性,如下所示。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Focused and not pressed -->
<item android:state_focused="true" 
      android:state_pressed="false" 
      android:drawable="#ffffff" />

<!-- Focused and pressed -->
<item android:state_focused="true" 
      android:state_pressed="true" 
      android:drawable="#000000" />

<!-- Unfocused and pressed -->
<item android:state_focused="false" 
      android:state_pressed="true" 
      android:drawable="#000000" />

<!-- Default color -->
<item android:drawable="#ffffff" />
</selector>

And then in the button itself do, note that this time the selector name is "button_background"然后在按钮本身做,注意这次选择器名称是“button_background”

android:background="@drawable/button_background" android:background="@drawable/button_background"

You have to do it in your code.你必须在你的代码中做到这一点。 Try this:试试这个:

    mBtn = ((Button) findViewById( R.id.button1 ));
    mBtn.setOnClickListener( new OnClickListener() {

        @Override
        public void onClick(View v) {
            mBtn.setTextColor( Color.RED );
        }
    });

Declare:宣布:

private Button mBtn;

You must set @drawable xml resource in textColor attributte您必须在textColor属性中设置@drawable xml 资源

Here is example: Android customized button;下面是示例: Android 自定义按钮; changing text color 改变文字颜色

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="false"
        android:color="#FFFFFF" />
    <item
        android:state_pressed="true"
        android:color="#000000" />
</selector>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM