目前我的视图中有一个按钮。我想要的是,当它被点击时,buttonbackgroundcolor变为ARGB为25,0,0,1,意味着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 ......
答案 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 将 implements 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;
}
答案 1 :(得分:0)