在Holo主题中,当你点击一个按钮时,它变成蓝色并闪耀一会儿。现在我希望按钮保持此外观,并在下一次单击时恢复正常外观。怎么做?
更新:我的代码:
public class HomeActivity extends SherlockActivity {
org.holoeverywhere.widget.Button bt;
boolean isPressed = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_layout);
bt = (org.holoeverywhere.widget.Button) findViewById(android.R.id.button1);
bt.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (!isPressed) {
isPressed = true;
} else {
isPressed = false;
}
bt.setPressed(isPressed);
}
return true;
}
});
}
}
答案 0 :(得分:1)
方法1:您可以为此按钮设置一个可绘制的选择器
1.Create a file `buttonbg.xml` in `drawable/`
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_focused="true"
android:drawable="@drawable/focused_button" />
<item
android:state_pressed="true"
android:drawable="@drawable/pressed_button" />
<item
android:drawable="@drawable/normal_button" />
</selector>
2. Create necessary images for indicating states of button
3. Set this buttonbg.xml as background for this button.
4. set this button setPressed(true) inside button click.
了解更多信息:How to modify the default button state in Android without affecting the pressed and selected states?
方法2:
boolean isPressed = false;
((Button)findViewById(R.id.button)).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
if(!isPressed){
isPressed = true;
}else{
isPressed = false;
}
((Button)findViewById(R.id.button)).setPressed(isPressed);
}
return true;
}
});
答案 1 :(得分:1)
首先,您可以从此位置获取SDK使用的默认图像(按钮,复选框,无论它是什么) - &gt;
〜\ Android的SDK \平台\机器人-18 \数据\水库\抽拉-HDPI
我在这里寻找API-18。使用您喜欢的任何支持Holo主题的版本。
从那里,您可以找到类似 btn_default_pressed_holo_dark.9.png 的图片。
将其复制粘贴到项目drawable-hdpi文件夹中。这是第一步。现在,
public class MainActivity extends Activity {
Button b;
Drawable back;
int flag=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(Button) findViewById(R.id.button1);
back=b.getBackground();
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(flag==0){
b.setBackgroundResource(R.drawable.btn_default_pressed_holo_dark);
flag=1;
}
else{
flag=0;
b.setBackground(back);
}
}
});
}
}
检查:)