我在一项活动中设置了4个RadioButton。
我遇到问题,当我点击“ALL”RadioButton时,它显示对应于“CATEGORIES”RadioButton的吐司。此外,当我点击“CATEGORIES”RadioButton时,它会显示对应于“ABOUT”RadioButton的toast。然后当我点击“关于”RadioButton时,没有任何显示。
我知道这与一些没有正确指导的事情有关,但似乎无法找出它是什么。
仅供参考:稍后我会用这些RadioButton打开活动,这就是为什么我还没有完成GetText而只使用了1个OnCheckedChangeListener
任何帮助将不胜感激!
MainActivity:
public class MainActivity extends Activity {
GridView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (GridView) findViewById(R.id.list);
adapter = new LazyAdapter(this, mStrings);
list.setAdapter(adapter);
RadioButton radioButton;
radioButton = (RadioButton) findViewById(R.id.btnAll);
radioButton.setOnCheckedChangeListener(btnAllOnCheckedChangeListener);
radioButton = (RadioButton) findViewById(R.id.btnAll);
radioButton
.setOnCheckedChangeListener(btnCategoriesOnCheckedChangeListener);
radioButton = (RadioButton) findViewById(R.id.btnCategories);
radioButton
.setOnCheckedChangeListener(btnPopularOnCheckedChangeListener);
radioButton = (RadioButton) findViewById(R.id.btnPopular);
radioButton.setOnCheckedChangeListener(btnAboutOnCheckedChangeListener);
radioButton = (RadioButton) findViewById(R.id.btnAbout);
}
private CompoundButton.OnCheckedChangeListener btnAllOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(), "Opened ALL tab", Toast.LENGTH_LONG).show();
}
}
};
private CompoundButton.OnCheckedChangeListener btnCategoriesOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(), "Opened CATEGORIES tab", Toast.LENGTH_LONG).show();
}
}
};
private CompoundButton.OnCheckedChangeListener btnPopularOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(), "Opened POPULAR tab", Toast.LENGTH_LONG).show();
}
}
};
private CompoundButton.OnCheckedChangeListener btnAboutOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(), "Opened ABOUT tab", Toast.LENGTH_LONG).show();
}
}
};
答案 0 :(得分:2)
// Replace this code and try
@Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (GridView) findViewById(R.id.list);
adapter = new LazyAdapter(this, mStrings);
list.setAdapter(adapter);
RadioButton btnAll = (RadioButton) findViewById(R.id.btnAll);
btnCategories.setOnCheckedChangeListener(btnAllOnCheckedChangeListener);
RadioButton btnCategories = (RadioButton) findViewById(R.id.btnCategories);
btnCategories.setOnCheckedChangeListener(btnCategoriesOnCheckedChangeListener);
RadioButton btnPopular = (RadioButton) findViewById(R.id.btnPopular);
btnPopular.setOnCheckedChangeListener(btnPopularOnCheckedChangeListener);
RadioButton btnAbout = (RadioButton) findViewById(R.id.btnAbout);
btnAbout.setOnCheckedChangeListener(btnAboutOnCheckedChangeListener);
}
答案 1 :(得分:0)
您重复所有单选按钮两次。代码应该是:
RadioButton radioButton;
radioButton = (RadioButton) findViewById(R.id.btnAll);
radioButton.setOnCheckedChangeListener(btnAllOnCheckedChangeListener);
radioButton = (RadioButton) findViewById(R.id.btnCategories);
radioButton
.setOnCheckedChangeListener(btnCategoriesOnCheckedChangeListener);
radioButton = (RadioButton) findViewById(R.id.btnPopular);
radioButton
.setOnCheckedChangeListener(btnPopularOnCheckedChangeListener);
radioButton = (RadioButton) findViewById(R.id.btnAbout);
radioButton.setOnCheckedChangeListener(btnAboutOnCheckedChangeListener);
答案 2 :(得分:0)
以这种方式制作单个CompoundButton.OnCheckedChangeListener:
private CompoundButton.OnCheckedChangeListener btnOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
switch buttonView{
case btn1:
//your logic
and so on
}
}
};
并为所有按钮应用此侦听器