安卓:单击时更改按钮的颜色 [英] Android: Change button color when clicked
问题描述
基本上,我试图创造一个按钮,点击时(注:不会 pressed)会改变颜色从颜色1至颜色2。当再次点击时,它会从COLOR2回COLOR1。
我已经寻找像疯了似的,我设法提取是如何改变颜色的按钮时,pressed,即当用户按下按钮(这code将在下面写的唯一信息)。但是,我想要的颜色改变,当用户点击(presses并释放)按钮,然后再改回来,一旦用户再次单击。
这文件是RES /绘制
<! - 改变颜色,当用户hols向下按钮 - >
< XML版本=1.0编码=UTF-8&GT?;
<选择
的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目的android:STATE_ pressed =真正的>
<形状>
<! - 改变颜色2 - >
< /形状>
< /项目>
<项目>
<形状>
<! - 改变颜色1 - >
< /形状>
< /项目>
< /选择器>
布尔TMP = FALSE;
button.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
TMP = TMP!;
v.setBackgroundColor(?TMP Color.RED:Color.BLUE);
}
});
编辑:显然你希望有一个更复杂的例子
首先创建一个可绘制的XML并将其命名为 pink_button.xml ,然后把里面的
以下code < XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:形状=矩形>
[固体机器人:颜色=#FF5EF1/>
<边角机器人:半径=15dp/>
<中风
机器人:宽=1DP
机器人:颜色=#303030/>
< /形状>
现在做出的 blue_button.xml
< XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:形状=矩形>
[固体机器人:颜色=#008DFF/>
<边角机器人:半径=15dp/>
<中风
机器人:宽=1DP
机器人:颜色=#303030/>
< /形状>
现在做一些演示活动的布局,我用的 button_demo_activity.xml
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
>
<按钮
机器人:ID =@ + ID / btnDemo
机器人:layout_width =150dp
机器人:layout_height =30dp
机器人:layout_centerInParent =真
机器人:layout_marginTop =100dp
机器人:背景=@可绘制/ pink_button
机器人:重力=中心
机器人:文本=PINK
机器人:文字颜色=@机器人:彩色/白
机器人:TEXTSIZE =15sp/>
< / RelativeLayout的>
和任何你想要的我用最后的活动,将其命名为 ButtonDemoActivity
公共类ButtonDemoActivity延伸活动{
私人按钮btnDemo;
私人布尔isPink = TRUE;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.button_demo_activity);
btnDemo =(按钮)findViewById(R.id.btnDemo);
btnDemo.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图查看){
isPink = isPink!;
INT渣油= isPink? R.drawable.pink_button:R.drawable.blue_button;
btnDemo.setBackgroundResource(渣油);
btnDemo.setText(?isPinkPINK:BLUE);
}
});
}
}
这是按钮的最终外观将在每个国家是什么
Basically, I'm trying to create a button that when clicked (note: NOT pressed) will change color from color1 to color2. When clicked again, it will change back from color2 to color1.
I have searched like crazy and the only information I managed to extract was how to change color when the button is pressed, that is, when the user holds down the button (this code will be written below). However, I want the color to change when the user clicks (presses and releases) the button, and then change back once the user clicks again.
This file is in res/drawable
<!-- Changes color when user hols down button -->
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<!-- change to color2 -->
</shape>
</item>
<item>
<shape>
<!-- change to color1 -->
</shape>
</item>
</selector>
boolean tmp = false;
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tmp = !tmp;
v.setBackgroundColor(tmp ? Color.RED : Color.BLUE);
}
});
EDIT: apparently you want to have a more complex example
First create a drawable XML in and name it pink_button.xml and place the following code inside
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FF5EF1"/>
<corners android:radius="15dp"/>
<stroke
android:width="1dp"
android:color="#303030"/>
</shape>
Now make a blue_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#008DFF"/>
<corners android:radius="15dp"/>
<stroke
android:width="1dp"
android:color="#303030"/>
</shape>
Now make some demo activity layout, I used button_demo_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/btnDemo"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:layout_marginTop="100dp"
android:background="@drawable/pink_button"
android:gravity="center"
android:text="PINK"
android:textColor="@android:color/white"
android:textSize="15sp"/>
</RelativeLayout>
And finally the activity, name it whatever you want I used ButtonDemoActivity
public class ButtonDemoActivity extends Activity {
private Button btnDemo;
private boolean isPink = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_demo_activity);
btnDemo = (Button) findViewById(R.id.btnDemo);
btnDemo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isPink = !isPink;
int resId = isPink ? R.drawable.pink_button : R.drawable.blue_button;
btnDemo.setBackgroundResource(resId);
btnDemo.setText(isPink ? "PINK" : "BLUE");
}
});
}
}
And this is what the final look of the buttons will be in each state
这篇关于安卓:单击时更改按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!