点击事件为所有动态生成的按钮android

时间:2013-11-06 21:41:47

标签: android android-layout android-listview

我编写代码来生成动态按钮,但我不知道如何动态地为每个按钮实现click事件。我找到了一些答案,但没有使用我的代码......请帮帮我.. 这是我的代码

public class dynamicbuttion extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        ScrollView sv = new ScrollView(this);
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        sv.addView(ll);

        for(int i = 0; i < 5; i++) {
            Button btn = new Button(this);
        btn.setId(i);
        btn.setText("dynamic buttion " + i);
            ll.addView(btn);

        }

        this.setContentView(sv);

    }
}

6 个答案:

答案 0 :(得分:4)

Button[] btn = new Button[5];
for(int i = 0; i < 5; i++) {
    btn[i] = new Button(this);
    btn[i].setId(i);
    btn[i].setText("dynamic buttion " + i);
    ll.addView(btn[i]);
    btn[i].setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //your desired functionality
    }
  });
}

答案 1 :(得分:4)

我使用自定义布局

完成了
private LinearLayout linearLayoutParent;

<强>的onCreate

linearLayoutParent = (LinearLayout)findViewById(R.id.linearLayoutParent);

现在,只要需要创建动态文字视图,我们就会添加父线性布局

TextView[] name = new TextView[10];
for (int i = 0; i < 10; i++) {
    View view = getLayoutInflater().inflate(R.layout.child_view, linearLayoutParent, false);
    name[i] = (TextView) view.findViewById(R.id.child_name);
    name[i].setText("Dynamic Textxview " + i);
    name[i].setId(i);
    name[i].setTag(String.valueOf(i));
    ll.addView(view);
    name[i].setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.i("Clicked", ""+v.getTag());
        }
    });
}

<强> child_view.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/child_name"
    android:layout_width="120dp"
    android:layout_height="50dp"
    android:background="#969696"
    android:gravity="center"
    android:text=""
    android:textColor="#fff"
    android:textSize="16sp" />

希望这会对你有所帮助。

答案 2 :(得分:0)

使用匿名的OnClickListener实现。像这样:

btn.setOnClickListener(new OnClickListener()
{
    public void onClick(View v)
    {
        doThisWhenClicked();
    }
});

答案 3 :(得分:0)

public class DynamicButton extends Activity {

    private LinearLayout ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dynic_button);

        ll = (LinearLayout) findViewById(R.id.llContent);
        Button[] dynamic_button = new Button[10];
        for (int i = 0; i < 10; i++) {
            dynamic_button[i] = new Button(this);
            dynamic_button[i].setId(i);
            dynamic_button[i].setTag("" + i);
            dynamic_button[i].setText("My Dynamic Button No: " + i);
            ll.addView(dynamic_button[i]);
            dynamic_button[i].setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Log.i("Clicked", "" + v.getTag());
                }
            });
        }

    }
}

答案 4 :(得分:0)

对于动态需求列表将是最佳选择,此外,您可以为动态绘图增加XML视图

try {
        int brotherCount =5;
        List<View> dynamicView = new ArrayList<>();
        List<TextInputEditText> dynamicBrotherText = new ArrayList<>();
        List<TextInputLayout> dynamicLayoutBrother = new ArrayList<>();
        for(int i=1;i<=brotherCount;i++){
            View family_static = getLayoutInflater()
                    .inflate(R.layout.my_family_static, mLayoutBrother, false);
            TextInputEditText  inputEditText=family_static.findViewById(R.id.TextInputEditTextFather);
            TextInputLayout inputLayout=family_static.findViewById(R.id.TextInputLayoutFamily);
            inputEditText.setText("name"+i);
            inputLayout.setHint(getResources().getString(R.string.about_my_brother));
            dynamicBrotherText.add(inputEditText);
            dynamicLayoutBrother.add(inputLayout);
            dynamicView.add(family_static);
            mLayoutBrother.addView(family_static);
        }
        for(View  view :dynamicBrotherText){
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showToast(((TextInputEditText)view).getText().toString());
                }
            });
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.d("", "createBrotherLayout: ");
    }

答案 5 :(得分:0)

我知道这篇文章很旧,但我最终在这里寻找答案,最终阅读了 setTag() 方法,它解决了我的问题。现在,我想添加一个我觉得比添加 setOnClickListener() 更简洁的答案,它看起来像这样:

  public void createButtonList(HashMap<String, Integer> buttonList){
    
    LinearLayout buttonLayout = findViewById(R.id.btnlyt);    
    for (Map.Entry<String, Integer> entry : buttonList.entrySet()) {
      String key = entry.getKey();

      Button button = new Button(this);
      button.setText(key);
      button.setTextSize(27);
      button.setTextColor(Color.rgb(150, 190, 200));
      button.setTypeface(Typeface.MONOSPACE);
      button.setTag(key);

      buttonLayout.addView(button);
      button.setOnClickListener(this);
    }
  }

  @Override
  public void onClick(View view) {
    String clickedButton = view.getTag().toString();
  }

所以在我的情况下,我有一个 HashMap,其中包含有关我想要在按钮上的文本的信息,这里重要的是 button.setOnClickListener(this); 并且也非常重要,在创建类时添加 implements View.OnClickListener 它看起来像这个:

public class ClassName extends AppCompatActivity implements View.OnClickListener