时人不识凌云木,直待凌云始道高。这篇文章主要讲述android动态添加复选框相关的知识,希望能为你提供帮助。
以下是我的布局xmls。现在我们在布局中有一个有限数量(七个)的复选框(listitem.xml)。我们将它添加到线性布局中。
main.xml中
<
?xml version="1.0" encoding="utf-8"?>
<
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
>
<
LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="21dp"
android:layout_marginTop="38dp" >
<
TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/linearLayout1"
android:layout_alignLeft="@+id/linearLayout1"
android:text="TESTER : "
android:textColor="#000000" />
<
AutoCompleteTextView
android:id="@+id/tester_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:capitalize="characters"
android:textColor="#000000">
<
requestFocus />
<
/AutoCompleteTextView>
<
/LinearLayout>
<
TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="APPS TO TEST : "
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"/>
<
View
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_below="@+id/textView2"
android:background="#CCCCCC"
android:layout_marginBottom="5dp"/>
<
ListView
android:id="@+id/apps_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/view1"
android:dividerHeight="5dp">
<
/ListView>
<
Button
android:id="@+id/refreshBtn"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentLeft="true"
android:text="Refresh" />
<
/RelativeLayout>
listitem.xml
<
?xml version="1.0" encoding="utf-8"?>
<
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="vertical" >
<
LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<
TextView
android:id="@+id/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="70"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<
Button
android:id="@+id/launch_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_weight="20"
android:text="LAUNCH" />
<
/LinearLayout>
<
RadioGroup
android:id="@+id/status_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<
RadioButton
android:id="@+id/pass_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="PASS"
android:textColor="#000000" />
<
RadioButton
android:id="@+id/fail_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FAIL"
android:textColor="#000000" />
<
/RadioGroup>
<
LinearLayout
android:id="@+id/failure_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<
TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:text="FAILURE REASONS"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<
LinearLayout
android:id="@+id/failure_reasonslist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="vertical" >
<
CheckBox
android:id="@+id/failure_Sound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
<
CheckBox
android:id="@+id/failure_GPS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="#000000" />
<
CheckBox
android:id="@+id/failure_Display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="#000000" />
<
CheckBox
android:id="@+id/failure_Network"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="#000000" />
<
CheckBox
android:id="@+id/failure_Rotation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="#000000" />
<
CheckBox
android:id="@+id/failure_Microphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="#000000" />
<
CheckBox
android:id="@+id/failure_Others"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="#000000" />
<
/LinearLayout>
<
/LinearLayout>
<
LinearLayout
android:id="@+id/lineatlayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<
TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DESCRIPTION : "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<
EditText
android:id="@+id/description_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="textMultiLine"
android:textColor="#000000" >
<
/EditText>
<
/LinearLayout>
<
LinearLayout
android:id="@+id/lineatlayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<
Button
android:id="@+id/submit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBMIT " />
<
Button
android:id="@+id/uninstall_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UNINSTALL" />
<
/LinearLayout>
<
!-- <
/RelativeLayout>
-->
<
/LinearLayout>
但是根据新要求,我们需要动态添加复选框。就像需要从API调用中获取选项并将其转换为视图中的复选框一样。我花了很多时间。有人可以建议一种方法来解决这个问题。
我们的布局应该是这样的。在列表视图中,我们显示员工详细信息,并且对于每个员工记录,应从API调用或数据库加载限定条件。
[Reviewer Name (Editable)]
Employee Lest
---------------------------------------
Employee1[Click Me]Male (0)Female ( )
Salary : [5999 (editable)]
Qualification
[X]BA
[ ]BSc
[ ]Matriculation
[ ]Other
[Submit Button]
---------------------------------------
Employee2[Click Me]Male (0)Female ( )
Salary : [5999 (editable)]
Qualification
[X]BA
[ ]BSc
[ ]Matriculation
[ ]Other
[Submit Button]
---------------------------------------
Employee3[Click Me]Male (0)Female ( )
Salary : [5999 (editable)]
Qualification
[X]BA
[ ]BSc
[ ]Matriculation
[ ]Other
[Submit Button]
---------------------------------------
源代码
private class ListAdapters extends ArrayAdapter<
ApplicationBean>
{
private ArrayList<
ApplicationBean>
items;
private int position;
public ListAdapters(Context context, int textViewResourceId,
ArrayList<
ApplicationBean>
mTitleList) {
super(context, textViewResourceId, mTitleList);
this.items = mTitleList;
}@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
this.position = position;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.applicationlistitem, null);
}final ApplicationBean o = (ApplicationBean) items.get(position);
if (o != null) {txtAppName = (TextView) v.findViewById(R.id.app_name);
txtAppName.setText("" + o.getAppName());
launchButton = (Button) v.findViewById(R.id.launch_btn);
launchButton.setTag(position);
launchButton.setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {final PackageManager pm = mContext.getPackageManager();
Intent LaunchIntent = pm
.getLaunchIntentForPackage(items
.get(Integer.parseInt(v.getTag()
.toString())).getPname());
mContext.startActivity(LaunchIntent);
}
});
final LinearLayout failure_reasonslist = (LinearLayout) v
.findViewById(R.id.failure_reasonslist);
rdgPassFail = (RadioGroup) v.findViewById(R.id.status_group);
rdgPassFail.setTag(position);
RadioButton passBtn = (RadioButton) v
.findViewById(R.id.pass_btn);
passBtn.setTag(position);
RadioButton failbtn = (RadioButton) v
.findViewById(R.id.fail_btn);
failbtn.setTag(position);
rdgPassFail
.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Override
public void onCheckedChanged(RadioGroup group,
int checkedId) {
ApplicationBean o = (ApplicationBean) items
.get(Integer.parseInt(group.getTag()
.toString()));
switch (checkedId) {
case R.id.fail_btn:
Log.e("Fail button", "Clicked");
o.setFailState(true);
o.setPassState(false);
numOptions = 0;
Log.e("Fail button--1", "Clicked");
break;
case R.id.pass_btn:
Log.e("Pass button", "Clicked");
o.setFailState(false);
o.setPassState(true);
Log.e("Pass button-----1", "Clicked");
break;
}
items.set(Integer.parseInt(group.getTag()
.toString()), o);
}});
LinearLayout featuresTable = (LinearLayout) v.findViewById(R.id.failure_reasonslist);
// use loop CheckBox feature1 = new CheckBox(this);
for(int i = 0;
i<
=5;
i++) {
CheckBox feature1 = new CheckBox(this.getContext());
featuresTable.addView(feature1);
}txtDescription = (EditText) v
.findViewById(R.id.description_text);
txtDescription.setTag(position);
if (txtDescription.isFocused()) {
InputMethodManager inputManager = (InputMethodManager) mContext
.getSystemService(INPUT_METHOD_SERVICE);
inputManager.restartInput(txtDescription);
}txtDescription
.setOnFocusChangeListener(new View.OnFocusChangeListener() {@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {final EditText Caption = (EditText) v;
o.setDescription(Caption.getText()
.toString());
}}
});
uninstallButton = (Button) v.findViewById(R.id.uninstall_btn);
uninstallButton.setTag(position);
// uninstallButton.setVisibility(View.INVISIBLE);
o.setUninstallVisible(false);
uninstallButton.setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {
Uri packageUri = Uri.parse("package:"
+ items.get(
Integer.parseInt(v.getTag().toString()))
.getPname());
Intent uninstallIntent = new Intent(
Intent.ACTION_DELETE, packageUri);
uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(uninstallIntent);
mTitleList.remove(items.get((Integer) v.getTag()));
mListView.setAdapter(new ListAdapters(mContext,
R.id.app_name, mTitleList));
((BaseAdapter) mListView.getAdapter())
.notifyDataSetChanged();
isUninstallclicked = true;
}
});
submitButton = (Button) v.findViewById(R.id.submit_btn);
submitButton.setTag(txtDescription);
submitButton.setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText tv = (EditText) v.getTag();
// get edittext
// objecttxtDescription = tv;
if (txtTesterName.getText().toString().equals("")) {
showDialog("Please enter the name of tester",
mContext);
} else if (numOptions == 0) {
showDialog("Please select failure reason", mContext);
} else if (tv.getText().toString().equals("")) {
showDialog("Please enter the description", mContext);
} else if (!isNetworkAvailable()) {showDialog(
"No network connection.Report won't be submitted",
mContext);
} else {if (!o.isUninstallVisible()) {
uninstallButton.setVisibility(View.VISIBLE);
o.setUninstallVisible(true);
mListView.invalidate();
}
PostRequest p = new PostRequest(Integer.parseInt(tv
.getTag().toString()));
p.execute();
}
}});
}
return v;
}
答案
LinearLayout featuresTable = (LinearLayout) findViewById(R.id.failure_reasonslist);
// use loop
CheckBox feature1 = new CheckBox(this);
featuresTable.addView(feature1 );
另一答案动态添加CheckBox
LinearLayout my_layout = (LinearLayout)findViewById(R.id.my_layout);
// Add Checkbox Dynamically
CheckBox checkBox = new CheckBox(mContext);
checkBox.setText(ETnewcheckbox.getText().toString()+"");
checkBox.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String msg = "You have " + (isChecked ? "checked" : "unchecked") + " this Check it Checkbox.";
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
// Add Checkbox to LinearLayout
if (my_layout != null) {
my_layout.addView(checkBox);
}
另一答案试试这个。
LinearLayout lLayout = (LinearLayout)findViewById(R.id.yourMainLayout);
CheckBox checkBox = new CheckBox(this);
// Here you will create CheckBox as many number of Checkbox you want. lLayout.addView(checkBox);
// Here you have to add this CheckBox to your layout.
【android动态添加复选框】祝好运。
推荐阅读
- 使用create-react-app时,为什么开发服务器会断开连接()
- Android Espresso - 如果未选中则单击复选框
- 获取请求在Angular 5 app中抛出404错误
- Android(请求关注通过WindowManager添加的视图(WebView))
- 将c#console app转换为c#windows form app
- Websphere Application Server异常ADMC0016E(系统无法创建SOAP连接器以连接到主机端口)
- 程序类型已存在(android.support.v7.util.AsyncListUtil $ 1)
- 在运行时更改Android Camera 2的Flash设置
- InApp订阅已过期,如何检查服务器端