【问题标题】:How to show an EditText on checking Check Box in Android Activity?如何在 Android Activity 中检查复选框时显示 EditText?
【发布时间】:2016-12-27 14:39:48
【问题描述】:

我想在我的 android 活动 中添加 check box 以便当 check box 被选中 时再添加一个 editText 应该出现在活动中.. 请告诉我该怎么做。

【问题讨论】:

  • 请说明您尝试过的方法以及遇到的具体问题。

标签: android android-activity checkbox


【解决方案1】:

默认取 2 个EditText 并使其可见Gone。然后检查复选框是选中还是未选中!如果选中,则使第二个EditText 的可见性Visible

【讨论】:

    【解决方案2】:

    设置一个监听器来执行这个功能..

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      • 2012-01-20
      相关资源
      最近更新 更多