gpt4 book ai didi

java - 使用单选按钮和复选框执行类似任务 (Android)

转载 作者:行者123 更新时间:2023-12-01 14:07:34 26 4
gpt4 key购买 nike

我有一个 EditText View 和一个复选框(隐藏),以及两个单选按钮(隐藏和显示)。按钮和复选框的功能是隐藏或显示EditView 的提示。我想第一个问题是我为什么要这样做。嗯,我对 Android 完全陌生,我只是在尝试不同的东西。

当我运行代码时, Activity 无法启动。在放置任何单选按钮之前,我测试了该程序,并且仅使用程序中的复选框就可以正常工作。所以我假设问题出在单选按钮的某个地方,或者是用于同一任务的两者的组合。

这是我的代码:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class EmPubLiteActivity extends Activity implements
CompoundButton.OnCheckedChangeListener {

CheckBox cb;
EditText et;
RadioButton rbHide;
RadioButton rbShow;
RadioGroup rg;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

cb = (CheckBox)findViewById(R.id.cbHideHint);
cb.setOnCheckedChangeListener(this);
et = (EditText)findViewById(R.id.editText1);

rbHide = (RadioButton)findViewById(R.id.rbHide);
rbShow = (RadioButton)findViewById(R.id.rbShow);

RadioGroup rg = new RadioGroup(this);
rg.addView(rbHide);
rg.addView(rbShow);
rbHide.setOnCheckedChangeListener(this);
rbShow.setOnCheckedChangeListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.em_pub_lite, menu);
return true;
}

@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {

switch (view.getId()) {

case R.id.cbHideHint:
if (isChecked) {
et.setHint(R.string.nothing);
rbHide.setChecked(true);
}
else {
et.setHint(R.string.text_here);
rbShow.setChecked(true);
}
break;
case R.id.rbHide:
et.setHint(R.string.nothing);
cb.setChecked(false);
break;
case R.id.rbShow:
et.setHint(R.string.text_here);
cb.setChecked(true);
}
}
}

我收到以下错误以及其他一些错误,但我感觉这就是问题所在:

java.lang.IllegalStateException: the specified child already has a parent. 
You must call removeView() on the child's parent first.

此错误意味着什么?如何修复它?

最佳答案

rbHide = (RadioButton)findViewById(R.id.rbHide);
rbShow = (RadioButton)findViewById(R.id.rbShow);

RadioGroup rg = new RadioGroup(this);
rg.addView(rbHide);
rg.addView(rbShow);
rbHide.setOnCheckedChangeListener(this);
rbShow.setOnCheckedChangeListener(this);

在布局 xml 中,rbHiderbShow 已经是其父 View 的 subview 。现在,您可以将它们添加到新创建的 View 中,而无需将它们从其父 View 中删除。您应该在布局 xml 中定义 RadioGroup,而不是在代码中创建它们。它应该看起来像这样:

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="@+id/rbHide" >

<RadioButton
android:id="@+id/rbHide"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<RadioButton
android:id="@+id/rbShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</RadioGroup>

关于java - 使用单选按钮和复选框执行类似任务 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18779801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com