[英]Change background color of button when clicked
目前,我的视图中有一个按钮。 我想要的是,单击按钮时,buttonbackgroundcolor更改为25,0,0,1的ARGB,表示不透明度为25%。 每次用户触摸按钮时,颜色应为该颜色。 因此,当不触摸按钮时,其背景应为100%透明。
这是我的.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方法:
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));
}
});
}
如您所见,我已经将背景设置为25,0,0,1。 问题是,当按下按钮时,颜色永久更改为25,0,0,1 ...
解决方案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;
}
}
});
解决方案2将此实现的OnTouchListener添加到您的类中,然后在onCreate()中将setOnTouchListener添加到您的按钮button.setOnTouchListener(this);中。 最后在下面的重写方法中添加此方法
@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;
}
仍然如果您有疑问请检查以下示例
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.