【发布时间】:2016-12-27 14:39:48
【问题描述】:
我想在我的 android 活动 中添加 check box 以便当 check box 被选中 时再添加一个 editText 应该出现在活动中.. 请告诉我该怎么做。
【问题讨论】:
-
请说明您尝试过的方法以及遇到的具体问题。
标签: android android-activity checkbox
我想在我的 android 活动 中添加 check box 以便当 check box 被选中 时再添加一个 editText 应该出现在活动中.. 请告诉我该怎么做。
【问题讨论】:
标签: android android-activity checkbox
默认取 2 个EditText 并使其可见Gone。然后检查复选框是选中还是未选中!如果选中,则使第二个EditText 的可见性Visible。
【讨论】:
设置一个监听器来执行这个功能..
public void addView(LinearLayout lay, EditText etxt){
lay.addView(etxt);
}
public EditText editText(Context context, int id, LinearLayout.LayoutParams params,int paddings){
EditText ib = new EditText(context);
ib.setId(id);
//ib.setBackgroundResource(Background);
// you can use this for a layout //
ib.setLayoutParams(params);
// you can use padding option by getting an array as argument
ib.setPadding(pad,pad,pad,pad);
return ib;
}
你可以这样使用:
LinearLayout main = (LinearLayout)findViewById(R.id.layout_main);
// Now Show the EditText inside the View
chkBox= (CheckBox)findViewById(R.id.Check_box);
satView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked){
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
EditText eTxt = editText(context,502/*id here*/,lp,0);
addView(main,eTxt);
}
}
});
【讨论】: