android 如何动态创建按钮并将点击事件添加到按钮

how to create button dynamically and add click event to button in android

我需要在 android 中的 activity 添加一个按钮。

当我点击这个按钮时,我需要得到哪个复选框被选中并且unchecked.My代码是

package com.example.a;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {

    @Override
    protected 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 < 20; i++) {
            CheckBox cb = new CheckBox(this);
            cb.setText("I'm dynamic!");
            ll.addView(cb);
            }
            this.setContentView(sv);


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
  LinearLayout ll = new LinearLayout(this);
   ll.setOrientation(LinearLayout.VERTICAL);  
    Button btn = new Button(this);
    btn.setText("Submit");
    btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    ll.addView(btn);
    sv.addView(ll);
    btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                 //code
    }
    });

看下面的可能对你有帮助:-

     LinearLayout ll = new LinearLayout(this);
     ll.setOrientation(LinearLayout.VERTICAL);

     Button btn = new Button(this);
     btn.setText("Submit");
     btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
     ll.addView(btn);
     sv.addView(ll);
     btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // implement the loop to get checked CheckBoxes
    }
    });

尝试使用 Arraylist

获取选中的复选框
 public class MainActivity extends AppCompatActivity {

private ArrayList<String> mCheckList=new ArrayList<String>();

@Override
protected 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 < 20; i++) {
        final CheckBox cb = new CheckBox(this);
        cb.setText("check"+i);
        ll.addView(cb);
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                {
                    mCheckList.add(cb.getText().toString());
                }
                else
                {
                    mCheckList.remove(cb.getText().toString());
                }
            }
        });

    }

    Button button=new Button(this);
    button.setText("Submit");
    ll.addView(button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,mCheckList.toString() +"CheckBox Select",Toast.LENGTH_SHORT).show();
        }
    });
    this.setContentView(sv);


}

更好的做事方式,您可以轻松地以更好的方式自定义它。

MainActivity.java

public class MainActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ScrollView sv = (ScrollView) findViewById(R.id.myscroll);
        LinearLayout ll = (LinearLayout) findViewById(R.id.mylinearLayout);

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        for (int i = 0; i < 20; i++) {

            View view = inflater.inflate(R.layout.coustom_check_box_design, null);
            CheckBox cb = (CheckBox) view.findViewById(R.id.checkBox);
            cb.setText("I'm dynamic!" + i);
            //cb.isChecked();
            ll.addView(view);
        }
    }
}

activity_main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/myscroll"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">


    <LinearLayout
        android:id="@+id/mylinearLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
</ScrollView>

coustom_check_box_design.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New CheckBox" />
</LinearLayout>