简体   繁体   English

如何在按一次按钮时更改文本颜色(Android)

[英]How do i change text color when button was pressed once (Android)

what i basically wanna do is this: When i click on the button i want its Text color to appear in a different color. 我基本上想要做的是:当我点击按钮时,我希望它的文字颜色以不同的颜色显示。 What i tried is this: 我试过的是这个:

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:color="@color/red" />
    <item
        android:state_pressed="false"
        android:color="#000" />
</selector>

and then i did use this selector as drawable on the button android:textColor 然后我确实在按钮android:textColor上使用此选择器作为drawable

but this doesn solve it since it only changes its color while i press the button. 但这并没有解决它,因为它只是按下按钮时改变颜色。 I want it like this: Default: black on click: blue on click again: black 我希望它像这样:默认:点击黑色:再次点击蓝色:黑色

any ideas how to do that? 任何想法如何做到这一点? :S :S

this is my shape for the button (if it matters): 这是按钮的形状(如果重要的话):

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="-1dp"
    android:insetLeft="-1dp"
    android:insetRight="-1dp">
    <selector>
        <item android:state_pressed="false">
            <shape android:shape="rectangle" >
                <corners
                    android:radius="0dp"
                    />
                <solid
                    android:color="@color/background_grey"
                    />
                <padding
                    android:left="0dp"
                    android:top="0dp"
                    android:right="0dp"
                    android:bottom="0dp"
                    />
                <size
                    android:width="100dp"
                    android:height="30dp"
                    />
                <stroke
                    android:width="1dp"
                    android:color="#ffb4b4b4"
                    />
            </shape>
        </item>

        <item android:state_pressed="true">
            <shape android:shape="rectangle" >
                <corners
                    android:radius="0dp"
                    />
                <solid
                    android:color="@color/pq_blue"
                    />
                <padding
                    android:left="0dp"
                    android:top="0dp"
                    android:right="0dp"
                    android:bottom="0dp"
                    />
                <size
                    android:width="100dp"
                    android:height="30dp"
                    />
                <stroke
                    android:width="1dp"
                    android:color="#ffb4b4b4"
                    />
            </shape>
        </item>
    </selector>
</inset>

thx in advance thx提前

EDIT 编辑

so i tried to do it programatically and tied the folowing just to see if it changes color´s ..but yea..it doesn´t (it seems like my onCLick event doesnt work): 所以我试图以编程方式进行操作并将下面的内容绑定,以确定它是否会改变颜色..但是......不是(似乎我的onCLick事件不起作用):

 @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.listview_item, container, false);



    final Button likeButton = (Button)rootView.findViewById(R.id.btLike);
    likeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String test = "tester";
            if(BUTTON_STATE==BUTTON_STATE_ONCE){

                likeButton.setTextColor(getResources().getColor(R.color.pq_blue));
                BUTTON_STATE = BUTTON_STATE_TWICE;
            }else{
                likeButton.setTextColor(getResources().getColor(R.color.red));
                BUTTON_STATE = BUTTON_STATE_ONCE;
            }

        }
    });
    return rootView;
}

} }

NOTE: i do all tht stuff in onCreateView since im in a Fragment of my ActionBarActivity(with tabs) if im doing it in the onCreate i get a null pointer exception at findViewById ( since it searches for the ID in my mainActivity, if im right?) 注意:我在onCreateView中执行所有操作,因为我在ActionBarActivity的片段中(带有选项卡)如果我在onCreate中执行它我会在findViewById获取空指针异常(因为它在我的mainActivity中搜索ID,如果我正确的话) ?)

so yea..any ideas? 所以...有什么想法?

Your textselector.xml - 你的textselector.xml -

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:color="@color/red" /> <!--selected text colour-->
    <item
        android:state_focused="true"
        android:color="@color/red" />
    <item            
        android:color="@color/blue" /> <!--unselected text colour-->
</selector>

Your button in layout.xml - 你在layout.xml中的按钮 -

<Button
    android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Exit"
    android:textColor="@drawable/textselector" <!-- SET textselector HERE -->
    android:background="@drawable/button_color"/>

You can use this code to do it programmatically: 您可以使用此代码以编程方式执行此操作:

myButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        ColorStateList myList=myButton.getTextColors();
        int myColor=myList.getDefaultColor();
        switch(myColor)
        {
            case Color.BLACK:
            myButton.setTextColor(Color.BLUE);
            break;

            case Color.BLUE:
            myButton.setTextColor(Color.BLACK);
            break;

       }
   }
});

If You want to have a behaviour like: 如果你想要有这样的行为:

  • First Click: Button text black 首先点击:按钮文字黑色
  • Second Click: Button text blue 第二次点击:按钮文字蓝色
  • third Click: button text black again 第三次点击:按钮文字再次变黑

I don´t think it´s possible with the selector, also not with state focused. 我不认为选择器是可能的,也不是关注状态。 Because if any other view will be clicked, the button is not focused anymore and will loose the textcolor, goes back to default. 因为如果单击任何其他视图,该按钮将不再聚焦并且将松开文本颜色,返回默认值。 You have to do it in a programmatically way: 您必须以编程方式执行此操作:

First, set the default textColor to what You want: black inside Your xml. 首先,将默认textColor设置为您想要的:在xml中设置黑色。 So than You have the color on no press. 所以没有按下你的颜色。 make a globa variabel to save the state: 制作一个全球变种来拯救国家:

 private int BUTTON_STATE = 0;
 private final int BUTTON_STATE_ONCE = 0;
 private final int BUTTON_STATE_TWICE = 1;



     button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {

          if(BUTTON_STATE==BUTTON_STATE_ONCE){

            button.setTextColor(Color.BLUE);
            BUTTON_STATE = BUTTON_STATE_TWICE;
          }else if(BUTTON_STATE==BUTTON_STATE_TWICE){

            button.setTextColor(Color.BLACK);
            BUTTON_STATE = BUTTON_STATE_ONCE;
          }

        }

    });

That´s just a possible solution, there are many ways.. 这只是一个可能的解决方案,有很多方法..

EDIT 编辑

for Your code: 为您的代码:

Create that global variables like i did in my example above, and use them in the if/else statement: 像我在上面的例子中那样创建全局变量,并在if / else语句中使用它们:

    likeButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

           if(BUTTON_STATE==BUTTON_STATE_ONCE){

        likeButton.setTextColor(getResources().getColor(R.color.pqBlue));
        BUTTON_STATE = BUTTON_STATE_TWICE;
      }else if(BUTTON_STATE==BUTTON_STATE_TWICE){

        likeButton.setTextColor(getResources().getColor(R.color.pqBlack));
        BUTTON_STATE = BUTTON_STATE_ONCE;
      }

    }
});

Try adding text_effect.xml drawable 尝试添加text_effect.xml drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:color="@color/white" /> <!-- pressed -->    
     <item android:color="@color/black" /> <!-- default -->
 </selector>

add this line in button control 在按钮控件中添加此行

android:textColor="@drawable/text_effect"

It will work. 它会工作。 Enjoy:) 请享用:)

If you do not mandatorily need a simple Button, you could use a ToggleButton that is made for binary-state handling... With a ToggleButton your selector would look like this : 如果你没有强制要求一个简单的Button,你可以使用一个用于二进制状态处理的ToggleButton ...使用ToggleButton你的选择器看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_checked="true" android:color="@color/red" />
   <!-- Default State -->
   <item android:color="#000" />
</selector>

It seems like you wanna implement something like toggle button. 看起来你想实现像切换按钮这样的东西。 Instead of button u can use toggle button. 而不是按钮你可以使用切换按钮。 Though if you wanna use button only then u need to do some changes in your java code as well as xml code also 虽然如果你只想使用按钮,那么你需要对你的java代码以及xml代码进行一些更改

Create a file def_btn.xml it will look like this.. 创建一个def_btn.xml文件,它看起来像这样..

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="rectangle" >
            <corners
                android:radius="0dp"
                />
            <solid
                android:color="@color/background_grey"
                />
            <padding
                android:left="0dp"
                android:top="0dp"
                android:right="0dp"
                android:bottom="0dp"
                />
            <size
                android:width="100dp"
                android:height="30dp"
                />
            <stroke
                android:width="1dp"
                android:color="#ffb4b4b4"
                />
        </shape>

Create another file press_btn.xml it will look like this.. 创建另一个文件press_btn.xml它看起来像这样..

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="rectangle" >
            <corners
                android:radius="0dp"
                />
            <solid
                android:color="@color/pq_blue"
                />
            <padding
                android:left="0dp"
                android:top="0dp"
                android:right="0dp"
                android:bottom="0dp"
                />
            <size
                android:width="100dp"
                android:height="30dp"
                />
            <stroke
                android:width="1dp"
                android:color="#ffb4b4b4"
                />
        </shape>

Inside your activity declare a private boolean variable(say isPressed) by default isPressed is false. 在您的活动内部声明一个私有布尔变量(比如isPressed),默认情况下isPressed为false。 & for default button background will be def_btn.xml &对于默认按钮背景将是def_btn.xml

Now write following in button's onClick event. 现在在按钮的onClick事件中写下以下内容。

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            isPressed = !isPressed;

            if(isPressed){
                btn.setBackgroundResource(R.drawable.def_btn);
            }else{
                btn.setBackgroundResource(R.drawable.press_btn);
            }
        }
    });

That's it.. 而已..

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

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