2015-02-23 75 views
-1

我想做一个考勤加任务系统。为此,我使用了复选框和线性布局。线性布局用于动态添加编辑文本框。当一个复选框被选中时,一个新的文本字段是可见的,当它被取消选中时,文本字段必须被隐藏。我已经使用的代码部分的东西像这样:现在如何在android中隐藏动态创建的EditText框?

public void onCheckBoxClicked(View v) { 
    boolean checked = ((CheckBox) v).isChecked(); 
    EditText tv; 
    tv = new EditText(this); 
    tv.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, 
       RadioGroup.LayoutParams.WRAP_CONTENT)); 
tv.setText(one.getText().toString()+ "'s task today?"); 
tv.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB 
tv.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom) 

switch (v.getId()) { 

case R.id.checkBox1: 
if(checked) { 
list.addView(tv); 
Toast.makeText(getApplicationContext(), one.getText().toString() + " is present", Toast.LENGTH_SHORT).show(); 
    } 

if(!checked) { 
tv.setVisibility(EditText.GONE); //problem exists here--is not executing 
Toast.makeText(getApplicationContext(), one.getText().toString() + " is absent", Toast.LENGTH_SHORT).show(); 
       } 

       break; 

,我可以很容易地得到一个编辑的文本字段当我检查盒子,但在取消选中它,编辑文本字段不躲。我该如何解决它?

+0

尝试View.GONE .. – rafid059 2015-02-23 10:01:02

+0

我试过了。视图也不起作用。 – 2015-02-23 10:04:30

+0

尝试使用'checkBox1.isChecked()'而不是'checked' – 2015-02-23 10:10:44

回答

0

这是因为您每次点击某个复选框时都会创建新的EditText。您正试图隐藏甚至没有显示/添加的EditText。得到它了!!

尝试使您的EditText tv = new EditText(this); editText作为类变量,在onCreate中创建它。然后当你需要时你可以隐藏/显示。

它看起来像下面的代码。

EditText tv; 
onCreate(.......){ 
.............. 

tv = new EditText(this); 
.............. 
} 

,然后使用这个tvonCheckBoxClicked方法,而不是创建一个在此方法。

+0

非常感谢。它解决了我的问题。 (Y) – 2015-02-23 10:10:52

+0

接受答案,如果它解决你的答案。快乐编码 – AAnkit 2015-02-23 10:13:46