12个按钮以编程方式 - 如何处理每次点击

时间:2014-08-01 09:45:38

标签: android android-layout

我创建了一个生成12个按钮的自定义LinearLayout视图。我希望能够处理它们的每个点击事件。这是我的cusom LinearLayout类:

public class SolutionButtons extends LinearLayout {

private static int AMOUNT_BUTTONS = 6;
private static int SIZE_BUTTONS = 130;
private static int PADDING = 13;

private ArrayList<Button> buttons;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public SolutionButtons(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}
public SolutionButtons(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}
public SolutionButtons(Context context) {
    super(context);
    init();
}

public ArrayList<Button> getButtons() {
    return buttons;
}

public void init() {

    buttons = new ArrayList<Button>();

    setOrientation(VERTICAL);
    for(int i = 0; i < 2; i++) {
        LinearLayout ll = new LinearLayout(getContext());
        ll.setOrientation(HORIZONTAL);
        for(int j = 0; j < AMOUNT_BUTTONS; j++) {
            Button b = new Button(getContext());
            LayoutParams layoutParams = new LayoutParams(SIZE_BUTTONS, SIZE_BUTTONS);
            layoutParams.setMargins(0, 0, PADDING, PADDING);
            b.setLayoutParams(layoutParams);
                    b.setBackground(getResources().getDrawable(R.drawable.solution_button_background));
            buttons.add(b);
            ll.addView(b);
        }
        addView(ll);
    }
}
}

我希望能够在按下时将每个按钮的可见性更改为GONE。我已经尝试通过在for循环中实现onClickListener创建的按钮,但它没有成功。有什么建议吗?

4 个答案:

答案 0 :(得分:2)

在您的课程后面添加一个方法:

private OnClickListener onClickListener = new OnClickListener() {

    @Override
    public void onClick(final View v) {
             switch(v.getId()) {
                 case R.id.but_arr_date:
                     break;
                 case R.id.but_arr_time:
                     break;
                 case R.id.but_update_arr:
                     break;
                 case R.id.but_fin_date:
                     break;
                 case R.id.but_fin_time:
                     break;
              }

    }
};

并添加

yourBut.setOnClickListener(onClickListener) to each button

答案 1 :(得分:1)

试试这个..

        for(int a = 0; a < taxi_type_spin.size(); a++){

               final Button rowTextView;
               rowTextView = new Button(getApplicationContext());
               rowTextView.setText(taxi_type_spin.get(a).taxi_type);
               rowTextView.setTextSize(15);
               rowTextView.setTextColor(getResources().getColor(R.color.white));
               LinearLayout.LayoutParams lparam = new LinearLayout.LayoutParams(0,
                       LinearLayout.LayoutParams.WRAP_CONTENT, 1);
               //rowTextView.setPadding(0, 0, 0, 0);      
               packageButtons.add(rowTextView);
               rowTextView.setLayoutParams(lparam);
               rowTextView.setId(a); /// Set ID to button like this .. 
               final int b = a;
               // get value of clicked item over here .. 

               rowTextView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Button btn = (Button)v;
                    String get_value = btn.getText().toString();
                    //Toast.makeText(getApplicationContext(), "Button name is : " + get_value + " AND ID IS : " + rowTextView.getId(), Toast.LENGTH_LONG).show();


                }
            });

              // add the textview to the linearlayout
              myLinearLayout.addView(rowTextView);
              setSelectedButtonColor(b);
              setSelectedButtonColor(0);
        }

希望它有所帮助!

答案 2 :(得分:0)

您可以使用

buttons.setId(j);

并检查

@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case 0:
            // So on

答案 3 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

public void init() {
   buttons = new ArrayList<Button>();
   setOrientation(VERTICAL);
   for(int i = 0; i < 2; i++) {
      LinearLayout ll = new LinearLayout(getContext());
      ll.setOrientation(HORIZONTAL);
      for(int j = 0; j < AMOUNT_BUTTONS; j++) {
          Button b = new Button(getContext());
          LayoutParams layoutParams = new LayoutParams(SIZE_BUTTONS, SIZE_BUTTONS);
          layoutParams.setMargins(0, 0, PADDING, PADDING);
          b.setLayoutParams(layoutParams);
          b.setBackground(getResources().getDrawable(R.drawable.solution_button_background));
          b.setOnClickListener(new OnClickListener() {
             @Override
              public void onClick(View v) {
                 v.setVisibility(GONE);
              }
          });
          buttons.add(b);
          ll.addView(b);
       }
       addView(ll);
   }
}