简体   繁体   English

在 Android 中创建自定义按钮

[英]Create a custom button in Android

I created a new class that extends Button:我创建了一个扩展 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
    }
}

In my main activity:在我的主要活动中:

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.

            }
        });
    }
    }
}

How can make it possible that, when I setOnClickListener() from my main class, the click() method is called automatically?当我从主类 setOnClickListener() 时,如何自动调用 click() 方法? In other words, this button is expected to act like any other button, but do something MORE when is clicked.换句话说,这个按钮应该像任何其他按钮一样工作,但在被点击时会做更多的事情。

https://github.com/johannilsson/android-actionbar/

and

this is the code for selector: this will be useful for u, name this file and put this in xml drawable folder,and set the background of ur button with this filename....这是选择器的代码:这对你很有用,命名这个文件并将它放在 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM