如何在显示android后更改自定义对话框中的视图的可见性
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在显示android后更改自定义对话框中的视图的可见性相关的知识,希望对你有一定的参考价值。
想要在显示对话框后单击某些内容来更改linearlayout的可见性。确定点击方法是真的。即使它没有点击方法也行不通。有可能吗?例:
singleA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
linearTYT.setVisibility(View.VISIBLE)
}else{
linearTYT.setVisibility(View.GONE)
}
}
});
它在对话框代码中。
答案
使用
LayoutInflator
你可能想要覆盖AlertDialog's
Button
因为它将默认dismiss()
onClick()
View view = getLayoutInflater().inflate(R.layout.your_inflated_layout, null);
final LinearLayout linearLayout = view.findViewById(R.id.your_linear);
// Now you can do whatever you want with LinearLayout
Dialog.OnShowListener showListener = new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
View.OnClickListener hideListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
linearLayout.setVisibility(View.INVISIBLE);
}
};
View.OnClickListener unHideListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
linearLayout.setVisibility(View.VISIBLE);
// Hiding LinearLayout but this doesn't
// dismiss the dialog if you want
// call dialogInterface.dismiss();
}
};
// Overriding AlertDialog's Button because we don't want it
// to dismiss in every click
Button hide = ((AlertDialog) dialogInterface).getButton(AlertDialog.BUTTON_POSITIVE);
Button unhide = ((AlertDialog) dialogInterface).getButton(AlertDialog.BUTTON_NEGATIVE);
hide.setOnClickListener(hideListener);
unhide.setOnClickListener(unHideListener);
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setView(view)
.setPositiveButton("Hide", null)
.setNegativeButton("Unhide", null);
AlertDialog dialog = builder.create();
dialog.setOnShowListener(showListener);
dialog.show();
以上是关于如何在显示android后更改自定义对话框中的视图的可见性的主要内容,如果未能解决你的问题,请参考以下文章