对不起,如果这太基础了。 我创建了一个扩展Button的新类:
public class MyButton extends Button {
public static final int STATUS_OFF = 0;
public static final int STATUS_PLAY1 = 1;
public static final int STATUS_PLAY2 = 2;
public static final int STATUS_PLAY3 = 3;
public int status;
private Context ctx;
public MyButton(Context context) {
super(context);
ctx = context;
status = STATUS_OFF;
super.setBackgroundResource(R.drawable.sound_button_off);
}
private void click() {
switch (status) {
case STATUS_OFF:
status = STATUS_PLAY1;
break;
case STATUS_PLAY1:
status = STATUS_PLAY2;
break;
case STATUS_PLAY2:
status = STATUS_OFF;
break;
case STATUS_PLAY3:
break;
}
// OTHER THINGS TO DO
}
}
在我的主要活动中:
public class MyActivity extends Activity {
private static final int HORIZ = 16;
private MyButton[] b = new MyButton[HORIZ];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rhythm);
createButtons();
}
private void createButtons() {
LinearLayout layout = (LinearLayout)findViewById(R.id.layout_main_linear);
for (int w=0; w<HORIZ; w++) {
b[w] = new MyButton(MyActivity.this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(b[w], p);
b[w].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// HELP ME HERE
// WHEN CLICKING THIS BUTTON, IT AUTOMATICALLY SHOULD RUN the private method click();
// IT IS PRIVATE AND SHOULD NOT BE RUN FROM HERE, BUT AUTOMATICALLY EACH TIME THE BUTTON IS CLICKED.
}
});
}
}
}
当我从主类中调用setOnClickListener()时,如何自动调用click()方法? 换句话说,此按钮应该像任何其他按钮一样,但在单击时执行更多操作。 感谢。
答案 0 :(得分:0)
https://github.com/johannilsson/android-actionbar/
和
这是选择器的代码:这对你有用,命名这个文件并把它放在xml drawable文件夹中,用这个文件名设置你的按钮背景....
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/focused" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/focusedpressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/pressed" />
<item android:drawable="@drawable/defaultbutton" />
</selector>