在自定义弹出窗口中设置了RadioGroup。RadioGroup中设置了7个RadioButton。
public class MyPopupWindow extends PopupWindow{
private View mRootLayout;
private OnKeyListener mOnKeyListener;
public MyPopupWindow(Context context){
LayoutInflater inflater = LayoutInflater.from(context);
mRootLayout = inflater.inflate(R.layout.dialog, null);//popupwindow的布局文件
setContentView(mRootLayout);
setWidth(LayoutParams.WRAP_CONTENT);
setHeight(LayoutParams.WRAP_CONTENT);
mRootLayout.setBackgroundDrawable(new ColorDrawable(Color.GRAY));
this.setElevation(1);
mOnKeyListener = new OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
Log.d("hzb", "MediaControlerPopupWindow --- OnKeyListener --- "+keyCode);
if(keyCode == KeyEvent.KEYCODE_BACK){
dismiss();
}
return true;
}
};
RadioGroup radgroup=(RadioGroup)mRootLayout.findViewById(R.id.dialogRadioGroup1);
mRootLayout.setFocusable(true);
mRootLayout.setFocusableInTouchMode(true);
radgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radbtn = (RadioButton) mRootLayout.findViewById(checkedId);
//上面这句加上了mRootLayout.这个radbtn才有了响应!关键点!
if(radbtn!=null){
switch (checkedId)
{case R.id.dialogRadioButton1:
radbtn.setChecked(true);
Toast.makeText(MainActivity.this,radbtn.getText().toString(),Toast.LENGTH_LONG).show();
break;
case R.id.dialogRadioButton2:
radbtn.setChecked(true);
Toast.makeText(MainActivity.this,radbtn.getText().toString(),Toast.LENGTH_LONG).show();
break;
case R.id.dialogRadioButton3:
radbtn.setChecked(true);
Toast.makeText(MainActivity.this,radbtn.getText().toString(),Toast.LENGTH_LONG).show();
break;
case R.id.dialogRadioButton4:
radbtn.setChecked(true);
Toast.makeText(MainActivity.this,radbtn.getText().toString(),Toast.LENGTH_LONG).show();
break;
case R.id.dialogRadioButton5:
radbtn.setChecked(true);
Toast.makeText(MainActivity.this,radbtn.getText().toString(),Toast.LENGTH_LONG).show();
break;
case R.id.dialogRadioButton6:
radbtn.setChecked(true);
Toast.makeText(MainActivity.this,radbtn.getText().toString(),Toast.LENGTH_LONG).show();
break;
case R.id.dialogRadioButton7:
radbtn.setChecked(true);
Toast.makeText(MainActivity.this,radbtn.getText().toString(),Toast.LENGTH_LONG).show();
break;
}
}
}
});
mRootLayout.setOnKeyListener(mOnKeyListener);
mRootLayout.getOnFocusChangeListener();
//mRootLayout.requestFocus();
}
}
出现了一种怪异的现象,就是点选一个RadioButton后,其相应的onCheckedChanged的事件响应了。但是这个RadioButton没有被选中的状态。困扰了一天,终于发现是mRootLayout.setBackgroundDrawable(new ColorDrawable(Color.GRAY));导致的!
关于popupwindows中RadioButton的问题把这个改为mRootLayout.setBackgroundDrawable(null);
关于popupwindows中RadioButton的问题
网友评论