我想实现关闭R.id.textSizeA
按钮,但它不起作用。有人可以帮忙吗?自定义警报对话框有许多用于其设置的单选按钮。当我按下单选按钮时,它应该关闭对话框,但我不知道该怎么做。
final LinearLayout textSizeBtn = (LinearLayout) findViewById(R.id.text_size_btn);
textSizeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(view.getContext());
final LayoutInflater layoutInflater = LayoutInflater.from(view.getContext());
final View view1 = layoutInflater.inflate(R.layout.font_size_dialog, null);
alertDialog.setView(view1);
RadioGroup radioGroup = (RadioGroup) view1.findViewById(R.id.textSizeChanGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
TextView sampleText = (TextView) view1.findViewById(R.id.texSize_sampleText);
if (checkedId == R.id.textSizeA) {
String size = String.valueOf(12);
sampleText.setTextSize(Float.parseFloat(size));
textSizeHint.setText(size);
SharedPreferences.Editor editor = view.getContext().getSharedPreferences(String.valueOf(R.string.textSizePref), MODE_PRIVATE).edit();
editor.putString("textSizeA", size);
editor.clear();
editor.apply();
} else if (checkedId == R.id.textSizeB) {
String size = String.valueOf(13);
sampleText.setTextSize(Float.parseFloat(size));
textSizeHint.setText(size);
SharedPreferences.Editor editor = view.getContext().getSharedPreferences(String.valueOf(R.string.textSizePref), MODE_PRIVATE).edit();
editor.putString("textSizeA", size);
editor.clear();
editor.apply();
}
}
});
alertDialog.create().show();
}
});
试试这个
改变
alertDialog.create().show();
自
AlertDialog alert = alertDialog.create();
alert.show();
现在使用alert
消除警报,如下所示
if (checkedId == R.id.textSizeA) {
String size = String.valueOf(12);
...
...
editor.apply();
alert.dismiss(); // add this
} else if
...
...
替换此行
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(view.getContext());
跟
final AlertDialog alertDialog = new AlertDialog.Builder(view.getContext());
而这个
alertDialog.create().show();
跟
alertDialog.show();
并在要关闭警报对话框时调用alertDialog.dismiss();