【问题标题】:Selector for a button which changes its background and text color更改其背景和文本颜色的按钮的选择器
【发布时间】:2014-06-24 10:16:09
【问题描述】:

我有一组 3 个按钮,希望在单击特定按钮时更改其颜色和文本,并在单击另一个按钮时再次恢复正常。

我是第一次使用选择器,我不清楚状态,任何人都可以帮助我解决这个问题。

这就是我正在做的事情。

这是我的 3 个按钮

<LinearLayout
    android:id="@+id/lineartab"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:gravity="center|center_horizontal"
    android:orientation="horizontal"
    android:weightSum="3" >

    <Button
        android:id="@+id/allMessagesBT"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/message_top_btn_selector"
        android:singleLine="false"
        android:text="All \nMessages"
        android:textColor="#7b818b"
        android:textSize="14sp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginBottom="3dp"
        android:layout_marginTop="3dp"
        android:background="@drawable/line" />

    <Button
        android:id="@+id/createNewBT"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/message_top_btn_selector"
        android:singleLine="false"
        android:text="Create \nNew"
        android:textColor="#7b818b"
        android:textSize="14sp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginBottom="3dp"
        android:layout_marginTop="3dp"
        android:background="@drawable/line" />

    <Button
        android:id="@+id/deleteBT"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/message_top_btn_selector"
        android:singleLine="false"
        android:text="Delete \nSelected"
        android:textColor="#7b818b"
        android:textSize="14sp" />
</LinearLayout>

这是我的选择器 xml

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/login_edittext" android:state_selected="true"></item>
<item android:drawable="@drawable/btn_accept_normal" android:state_pressed="true</item>
<item android:drawable="@drawable/btn_accept_hover"></item>

 </selector>

我想做的是在单击按钮后更改背景颜色并保持这种状态直到单击另一个按钮

【问题讨论】:

  • 发布实际代码以更改您尝试过的文本和颜色..
  • 我认为你必须在代码中使用 setclickListener for Button
  • 当我单击按钮时,它会暂时更改其背景,然后恢复正常,但我想保持背景更改,直到按下下一个按钮。我可以用选择器来做还是我需要用 JAVA 编码来做
  • 有什么办法可以使用选择器,因为我需要在大多数地方实现这一点
  • 您可以在您想要的所有类中创建一种处理此问题的方法,获取一组视图作为参数,并且必须更改女巫位置的一个索引,然后设置背景或您想要的任何内容视图

标签: android button selector onclicklistener


【解决方案1】:

按照 Devil Abhi 的建议尝试,并根据需要添加一些代码,以在单击特定按钮时更改其颜色和文本,并在单击另一个按钮时再次恢复正常。

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){

    case R.id.button1:
        b1.setBackgroundColor(Color.RED);
        b1.setTextColor(Color.WHITE); 
        b1.setTypeface(null, Typeface.BOLD);
        b2.setBackgroundColor(*default background color*);
        b3.setBackgroundColor(*default background color*);
        break;

    case R.id.button2:
        b2.setBackgroundColor(Color.GREEN); 
        b2.setTextColor(Color.WHITE);
        b2.setTypeface(null, Typeface.BOLD);
        b1.setBackgroundColor(*default background color*);
        b3.setBackgroundColor(*default background color*);
        break;

    case R.id.button3:
        b3.setBackgroundColor(Color.BLUE);
        b3.setTextColor(Color.WHITE);
        b3.setTypeface(null, Typeface.BOLD);
        b1.setBackgroundColor(*default background color*);
        b2.setBackgroundColor(*default background color*);
        break;

    }

【讨论】:

  • 如何更改文字样式??
  • @Dude 按钮的文字样式??
  • 我的意思是说我想让按钮的文本在点击时变为粗体
  • 只需添加这个 b1.setTypeface(null, Typeface.BOLD);在按钮的点击事件中。
  • @Dude 查看编辑后的答案,使按钮的文本在点击时变为粗体。
【解决方案2】:

您需要使用 RadioButtons 或 ToggleButton 而不是按钮。在您的选择器中,您需要为 state_checked 定义背景并控制您在代码中的状态(对于切换按钮,对于 radioGroup 内的单选按钮不是必需的)。

当你按下另一个按钮时,我建议使用toggle button 并将检查设置为 false。

【讨论】:

    【解决方案3】:

    试试这个...

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" 
        android:id="@+id/relative1">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="49dp"
            android:layout_marginTop="39dp"
            android:text="Red" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button1"
            android:layout_below="@+id/button1"
            android:layout_marginTop="34dp"
            android:text="Green" />
    
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/button2"
            android:layout_centerVertical="true"
            android:text="Blue" />
    
    </RelativeLayout>
    

    package com.example.backgroundcolor;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.graphics.Color;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.RelativeLayout;
    
    public class MainActivity extends Activity implements OnClickListener {
    
        RelativeLayout rl;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            rl = (RelativeLayout)findViewById(R.id.relative1);
            Button b1 = (Button)findViewById(R.id.button1);
            Button b2 = (Button)findViewById(R.id.button2);
            Button b3 = (Button)findViewById(R.id.button3);
            b1.setOnClickListener(this);
            b2.setOnClickListener(this);
            b3.setOnClickListener(this);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch(v.getId()){
    
            case R.id.button1:
                rl.setBackgroundColor(Color.RED);
                break;
    
            case R.id.button2:
                rl.setBackgroundColor(Color.GREEN);
                break;
    
            case R.id.button3:
                rl.setBackgroundColor(Color.BLUE);
                break;
    
            }
        }
    
    }
    

    【讨论】:

    • @blackbelt 在简单按钮上单击其设置背景
    • 我不想在 java 文件中执行此操作,我想通过选择器执行此操作。 t=可能通过选择器
    • 你想使用切换按钮吗?
    【解决方案4】:

    试试这个方法,希望能帮助你解决问题。

    radio_button_background_selector.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@android:color/darker_gray" android:state_checked="true" android:state_pressed="false"/>
        <item android:drawable="@android:color/white" android:state_checked="false" android:state_pressed="false"/>
        <item android:drawable="@android:color/darker_gray" android:state_checked="true" android:state_pressed="true"/>
        <item android:drawable="@android:color/white" android:state_checked="false" android:state_pressed="true"/>
    </selector>
    

    radio_button_text_selector.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@android:color/white" android:state_checked="true"></item>
        <item android:color="@android:color/black"></item>
    </selector>
    
    
    <LinearLayout
            android:id="@+id/lineartab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <RadioButton
                android:id="@+id/allMessagesBT"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:gravity="center"
                android:background="@drawable/radio_button_background_selector"
                android:textColor="@drawable/radio_button_text_selector"
                android:text="All \nMessages"
                android:textSize="14sp"
                android:checked="true"/>
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginBottom="3dp"
                android:layout_marginTop="3dp"
                android:background="@drawable/line" />
    
            <RadioButton
                android:id="@+id/createNewBT"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:gravity="center"
                android:background="@drawable/radio_button_background_selector"
                android:textColor="@drawable/radio_button_text_selector"
                android:singleLine="false"
                android:text="Create \nNew"
                android:textSize="14sp" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginBottom="3dp"
                android:layout_marginTop="3dp"
                android:background="@drawable/line" />
    
            <RadioButton
                android:id="@+id/deleteBT"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:button="@null"
                android:gravity="center"
                android:background="@drawable/radio_button_background_selector"
                android:textColor="@drawable/radio_button_text_selector"
                android:text="Delete \nSelected"
                android:textSize="14sp" />
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2012-05-30
      • 2022-01-13
      • 2013-10-24
      • 1970-01-01
      • 2014-02-10
      • 1970-01-01
      • 2020-11-07
      • 2011-10-03
      • 1970-01-01
      相关资源
      最近更新 更多