【问题标题】:I would like to make a button selected using selector item with shape in android我想在android中使用具有形状的选择器项选择一个按钮
【发布时间】:2015-03-16 04:41:30
【问题描述】:

我已经使用带形状的选择器定义了按钮样式,并在可绘制文件夹中保持不变。然后将其与布局文件中的按钮一起附加。

按下按钮时按钮背景颜色已更改,但释放按钮后显示默认颜色。我想要的是单击按钮后应保留所选颜色。我需要帮助。提前致谢。

drawable/highscore_button_style.xml 中的按钮样式

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" >
        <shape android:shape="rectangle" >
            <solid android:color="@color/high_score_btn_bg_selected" />
            <corners android:radius="5dp" />
            <stroke android:color="@color/high_score_btn_bg_selected" android:width="1dp" />
        </shape>
    </item>
    <item android:state_pressed="true" >
        <shape android:shape="rectangle" >
            <solid android:color="@color/high_score_btn_bg_selected" />
            <corners android:radius="5dp" />
            <stroke android:color="@color/high_score_btn_bg_selected" android:width="1dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/high_score_btn_bg" />
            <corners android:radius="5dp" />
            <stroke android:color="@color/high_score_btn_bg" android:width="1dp" />
        </shape>
    </item>
</selector>

布局文件中的按钮:

<Button
    android:id="@+id/btnLevel1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/highscore_button_style"
    android:layout_marginRight="1dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:layout_weight="1"
    android:text="1"
    android:textColor="@color/high_score_btn_text" />
<Button
    android:id="@+id/btnLevel2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/highscore_button_style"
    android:layout_marginRight="1dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:layout_weight="1"
    android:text="2"
    android:textColor="@color/high_score_btn_text" />
<Button
    android:id="@+id/btnLevel3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/highscore_button_style"
    android:layout_marginRight="1dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:layout_weight="1"
    android:text="3"
    android:textColor="@color/high_score_btn_text" />

【问题讨论】:

    标签: android selector shape


    【解决方案1】:

    在.java文件中添加以下代码

    Button btn =(Button) findViewById(R.id.btnLevel1);
    btn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        v.setSelected(true);
        // code here 
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多