[英]Change background color of button when clicked
At the moment I have a button in my view. 目前,我的视图中有一个按钮。 What I want is that when it gets clicked that the buttonbackgroundcolor changes to an ARGB of 25,0,0,1 , meaning 25% opacity.
我想要的是,单击按钮时,buttonbackgroundcolor更改为25,0,0,1的ARGB,表示不透明度为25%。 It should be that color every time the user touches the button.
每次用户触摸按钮时,颜色应为该颜色。 So when it is not touched the button's background should be 100% transparent.
因此,当不触摸按钮时,其背景应为100%透明。
Here's my .xml 这是我的.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity2"
android:background="#ffdb4b5e">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#ffffffff"
android:textSize="72sp"
android:background="#00000000"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/thing1"
android:id="@+id/textView"
android:textColor="#ffffffff"
android:textSize="36sp"
android:gravity="center"
android:layout_above="@+id/button"
/>
</RelativeLayout>
onClick method: onClick方法:
n.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Random RAND = new Random();
int position = RAND.nextInt(colors.length);
int position2 = (index++);
String nextValue = colors[position];
String textValue = values[position2];
tv.setText(textValue);
rl.setBackgroundColor(Color.parseColor(nextValue));
n.setBackgroundColor(Color.argb(25,0,0,1));
}
});
}
as you can see I already set the background to 25,0,0,1. 如您所见,我已经将背景设置为25,0,0,1。 The problem is though that when the button gets pressed the color changes permanently to 25,0,0,1...
问题是,当按下按钮时,颜色永久更改为25,0,0,1 ...
Solution 1 Easy way 解决方案1简单方法
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Do your work here
button.setBackgroundColor(Color.RED);
return true;
case MotionEvent.ACTION_UP:
button.setBackgroundColor(Color.BLUE);
return true;
default:
return false;
}
}
});
Solution 2 Add this implements OnTouchListener to your class then in onCreate() add the setOnTouchListener to your button button.setOnTouchListener(this); 解决方案2将此实现的OnTouchListener添加到您的类中,然后在onCreate()中将setOnTouchListener添加到您的按钮button.setOnTouchListener(this);中。 and finally add this below override method
最后在下面的重写方法中添加此方法
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
if (v == button) {
//do your work here
button.setBackgroundColor(Color.BLACK);
}
} else if (action == MotionEvent.ACTION_UP) {
button.setBackgroundColor(Color.GRAY);
//}
}
return true;
}
Still if u have doubt check the below Example 仍然如果您有疑问请检查以下示例
public class MainActivity extends ActionBarActivity implements OnTouchListener{
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
button.setOnTouchListener(this);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
if (v == button) {
button.setBackgroundColor(Color.BLACK);
}
} else if (action == MotionEvent.ACTION_UP) {
button.setBackgroundColor(Color.GRAY);
}
return true;
}
To have the button change when "pressed" and then change back when the user stops pressing it you need a selector: 要在“按下”时更改按钮,然后在用户停止按下时将其更改回您需要一个选择器:
android button selector android按钮选择器
It handles "focused" and other things too. 它也处理“聚焦”和其他事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.