问题
I need to add a button to the activity in android.
And when I click this button I need to get which checkbox is checked and unchecked.My code is
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;
}
}
回答1:
Better way of doing and you can easily customize it in better way.
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>
回答2:
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
}
});
回答3:
See the below one it may help you:-
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
}
});
回答4:
Try this to get the THe selected Checkbox using 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);
}
来源:https://stackoverflow.com/questions/38783393/how-to-create-button-dynamically-and-add-click-event-to-button-in-android