Android:以编程方式隐藏和显示LinearLayout和EditText



我有一个线性布局和一个复选框中的四个编辑文本。根据其他条件和检查/取消检查,我想隐藏并显示它们。如何以编程方式执行此操作?提前感谢任何帮助。

使用了下面的代码,但当我选中复选框时它对我不起作用。

CheckBox chkSetPos= (CheckBox) findViewById(R.id.checkBox1);
          chkSetPos.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    layPos = (LinearLayout) findViewById(R.id.layoutPos);
                    layPos.setVisibility(LinearLayout.VISIBLE);             
                }
                else
                    layPos = (LinearLayout) findViewById(R.id.layoutPos);
                    layPos.setVisibility(LinearLayout.GONE);                      
              }
            });

XML文件:

<CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="9dp"
        android:text="@string/use_custom_position_fix"
        android:textSize="15sp" />
    <LinearLayout
        android:id="@+id/layoutPos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <EditText
            android:id="@+id/lowerLeftX"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:ems="10"
            android:hint="@string/lower_left_x_fix"
            android:inputType="text" />
        <EditText
            android:id="@+id/lowerLeftY"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:ems="10"
            android:hint="@string/lower_left_y_fix"
            android:inputType="text" />
        <EditText
            android:id="@+id/upperRightX"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:ems="10"
            android:hint="@string/upper_right_x_fix"
            android:inputType="text" />
        <EditText
            android:id="@+id/upperRightY"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:ems="10"
            android:hint="@string/upper_right_y_fix"
            android:inputType="text" />
    </LinearLayout>
Check out this
final LinearLayout search_bar=(LinearLayout) findViewById(R.id.search_bar);
    CheckBox check=(CheckBox) findViewById(R.id.checkBox1);
    check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked==true)
            {
                search_bar.setVisibility(View.GONE);
            }
            else
            {
                search_bar.setVisibility(View.VISIBLE);
            }
        }
    });

您尝试使用 onCheckedChangeListener 而不是 onClickListener。

CheckBox chkSetPos= (CheckBox) findViewById(R.id.checkBox1);
      chkSetPos.setOnCheckedChangeListener( new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            if (isChecked) {
                layPos = (LinearLayout) findViewById(R.id.layoutPos);
                layPos.setVisibility(LinearLayout.VISIBLE);             
            }
            else
                layPos = (LinearLayout) findViewById(R.id.layoutPos);
                layPos.setVisibility(LinearLayout.GONE);                      
          }
        }
    })

最新更新