如何动态创建按钮并将click事件添加到android中的按钮

时间:2016-08-05 07:18:51

标签: android onclicklistener android-button

我需要在android中为活动添加一个按钮。

当我点击此按钮时,我需要选中哪个复选框并取消选中。我的代码是

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;
    }

}

4 个答案:

答案 0 :(得分:0)

$this->db->where($whereField2." = ",$data['get_JobPan_Data'][$i]->QCJobPanelId);

答案 1 :(得分:0)

请参阅以下内容,它可能会对您有所帮助: -

    let alphabetArray = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
    let arr = ["1","2","3","3","2","3"]
    var newArr = [String]()
    newArr = arr.map { (item) in
        alphabetArray[Int(item)! - 1]
    }
    print(newArr)

答案 2 :(得分:0)

尝试使用Arraylist

选择Checkbox
 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);


}

答案 3 :(得分:0)

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

<强> 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>