android复选框显示和隐藏
单击要显示linearlayout7、linearlayout8、linearlayout9的复选框时,linearlayout6中有一个复选框。否则我想隐藏这3个线性布局(7,8,9) 复选框android复选框显示和隐藏,android,checkbox,Android,Checkbox,单击要显示linearlayout7、linearlayout8、linearlayout9的复选框时,linearlayout6中有一个复选框。否则我想隐藏这3个线性布局(7,8,9) 复选框 <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/checkbox1" /> XML 在xml布局中添加三个复选框,就像对复选框1所做
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox1"
/>
XML
在
xml
布局中添加三个复选框,就像对复选框1
所做的那样,然后为它们指定id
CheckBox checkbox=(CheckBox)findViewById(R.id.checkBoxInLayout6);
public void addListenerOnChk() // for checkbox
{
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked)
{
checkBoxInLayout7.setVisibility(View.VISIBLE);
checkBoxInLayout8.setVisibility(View.VISIBLE);
checkBoxInLayout9.setVisibility(View.VISIBLE);
}
else
{
checkBoxInLayout7.setVisibility(View.INVISIBLE);
checkBoxInLayout8.setVisibility(View.INVISIBLE);
checkBoxInLayout9.setVisibility(View.INVISIBLE);
}
}
});
}
在xml
布局中添加三个复选框,就像对复选框1
所做的那样,然后为它们指定id
CheckBox checkbox=(CheckBox)findViewById(R.id.checkBoxInLayout6);
public void addListenerOnChk() // for checkbox
{
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked)
{
checkBoxInLayout7.setVisibility(View.VISIBLE);
checkBoxInLayout8.setVisibility(View.VISIBLE);
checkBoxInLayout9.setVisibility(View.VISIBLE);
}
else
{
checkBoxInLayout7.setVisibility(View.INVISIBLE);
checkBoxInLayout8.setVisibility(View.INVISIBLE);
checkBoxInLayout9.setVisibility(View.INVISIBLE);
}
}
});
}
这是解决办法
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
linearlayout7.setVisibility(View.VISIBLE);
linearlayout8.setVisibility(View.VISIBLE);
linearlayout9.setVisibility(View.VISIBLE);
}else {
linearlayout7.setVisibility(View.GONE);
linearlayout8.setVisibility(View.GONE);
linearlayout9.setVisibility(View.GONE);
}
}
});
只是简单的代码。
很乐意帮忙以下是解决方案
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
linearlayout7.setVisibility(View.VISIBLE);
linearlayout8.setVisibility(View.VISIBLE);
linearlayout9.setVisibility(View.VISIBLE);
}else {
linearlayout7.setVisibility(View.GONE);
linearlayout8.setVisibility(View.GONE);
linearlayout9.setVisibility(View.GONE);
}
}
});
只是简单的代码。
很乐意帮助您试试这些:
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
ll7.setVisibility(View.VISIBLE);
ll8.setVisibility(View.VISIBLE);
ll9.setVisibility(View.VISIBLE);
}
else
{
ll7.setVisibility(View.GONE);
ll8.setVisibility(View.GONE);
ll9.setVisibility(View.GONE);
}
}
});
试试这些:
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
ll7.setVisibility(View.VISIBLE);
ll8.setVisibility(View.VISIBLE);
ll9.setVisibility(View.VISIBLE);
}
else
{
ll7.setVisibility(View.GONE);
ll8.setVisibility(View.GONE);
ll9.setVisibility(View.GONE);
}
}
});
试着简单一点
试着简单一点。1.添加
android:onClick="onCheckboxClicked"
属性,然后将下面的代码添加到.java文件中
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkbox_meat:
if (checked)
showLayout789();
//do things when checked
else
removeLayout789();
// do things when unchecked
break;
}
}
现在使用布局的id查找布局并设置其可见性
private void showLayout789(){
View v9 = findViewById(R.id.screen4layout9);
v9.setVisibility(View.VISIBLE);
View v8 = findViewById(R.id.screen4layout8);
v8.setVisibility(View.VISIBLE);
View v7 = findViewById(R.id.screen4layout7);
v7.setVisibility(View.VISIBLE);
}
使不可见
private void removeLayout789(){
View v9 = findViewById(R.id.screen4layout9);
v9.setVisibility(View.GONE);
View v8 = findViewById(R.id.screen4layout8);
v8.setVisibility(View.GONE);
View v7 = findViewById(R.id.screen4layout7);
v7.setVisibility(View.GONE);
}
1.加入
属性,然后将下面的代码添加到.java文件中
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkbox_meat:
if (checked)
showLayout789();
//do things when checked
else
removeLayout789();
// do things when unchecked
break;
}
}
现在使用布局的id查找布局并设置其可见性
private void showLayout789(){
View v9 = findViewById(R.id.screen4layout9);
v9.setVisibility(View.VISIBLE);
View v8 = findViewById(R.id.screen4layout8);
v8.setVisibility(View.VISIBLE);
View v7 = findViewById(R.id.screen4layout7);
v7.setVisibility(View.VISIBLE);
}
使不可见
private void removeLayout789(){
View v9 = findViewById(R.id.screen4layout9);
v9.setVisibility(View.GONE);
View v8 = findViewById(R.id.screen4layout8);
v8.setVisibility(View.GONE);
View v7 = findViewById(R.id.screen4layout7);
v7.setVisibility(View.GONE);
}
此外,你还需要默认隐藏布局的可见性。我认为如果你知道或编写android代码,这是非常容易的,那么你肯定知道如何在android中隐藏和显示任何视图。但是如果你不知道android开发,那么从一些对你来说很困难的地方选择这些代码,有任何答案解决了你的问题吗?如果是,请标记它。Thanks@JohnJoe非常感谢。工作正常。@Hareshchelana我是android新手:D。我知道代码是如何工作的,但对linearlayout不太确定。另外,你需要默认隐藏布局的可见性。我认为这很容易,如果你知道或编写android代码,那么你肯定知道如何在android中隐藏和显示任何视图,但如果你不知道android开发,那么选择这个从某些地方输入的代码对您来说很困难,有没有解决您的问题的答案?如果是,请标记它。Thanks@JohnJoe非常感谢。工作正常。@Hareshchelana我是android的新手:D。我知道代码是如何工作的,但对linearlayout不太清楚。谢谢你帮助我,谢谢你帮助我,谢谢你帮助我,谢谢你帮助我