安卓绑定switch 控制输入内容显示与隐藏
时间: 2023-12-07 08:05:02 浏览: 30
可以通过在Android中使用Switch和EditText控件来实现控制输入内容的显示与隐藏。你可以通过监听Switch的状态变化来决定EditText是否可编辑或者可见。以下是一个简单的示例代码:
在布局文件中添加Switch和EditText控件:
```
<Switch
android:id="@+id/switch_hide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隐藏内容"
android:checked="false" />
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:visibility="visible"
android:enabled="true" />
```
在Activity中添加Switch状态监听器,根据Switch状态变化来设置EditText的可编辑和可见性:
```
Switch switchHide = findViewById(R.id.switch_hide);
final EditText editText = findViewById(R.id.edit_text);
switchHide.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
editText.setEnabled(false);
editText.setVisibility(View.INVISIBLE);
} else {
editText.setEnabled(true);
editText.setVisibility(View.VISIBLE);
}
}
});
```
这样,当Switch状态改变时,EditText的可编辑和可见性也会随之改变。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)