Java 对话框片段上的“不再显示”复选框
所以标题解释了这一切。我有一个对话框片段,当前弹出,我想添加一个不显示复选框到它,然后很明显地实现检查,不显示,如果它被选中。我知道有一个.setSingleChoiceItems,但我不完全确定其中会有什么,因为我不会在某个地方添加它。但我可能又错了,因为我刚刚进入Android开发阶段 Dialogfragment javaJava 对话框片段上的“不再显示”复选框,java,android,checkbox,android-dialogfragment,dialogfragment,Java,Android,Checkbox,Android Dialogfragment,Dialogfragment,所以标题解释了这一切。我有一个对话框片段,当前弹出,我想添加一个不显示复选框到它,然后很明显地实现检查,不显示,如果它被选中。我知道有一个.setSingleChoiceItems,但我不完全确定其中会有什么,因为我不会在某个地方添加它。但我可能又错了,因为我刚刚进入Android开发阶段 Dialogfragment java public class WifivsDataDialog extends DialogFragment { @ Override p
public class WifivsDataDialog extends DialogFragment {
@
Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_box)
.setPositiveButton(R.string.WiFi, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.Cell_Data, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
下面是在my MainActivity.java中调用它的代码
WifivsDataDialog myDiag = new WifivsDataDialog();
myDiag.show(getFragmentManager(), "dialog_layout");
myDiag.setCancelable(false);
DialogFragment类
public class WifivsDataDialog extends DialogFragment implements View.OnClickListener {
private CheckBox checkBox;
private Button button1;
private Button button2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mainView = inflater.inflate(R.layout.wifi_dialog, container);
checkBox = (CheckBox) mainView.findViewById(R.id.checkBox);
button1 = (Button) mainView.findViewById(R.id.button1);
button2 = (Button) mainView.findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
// Store the isChecked to Preference here
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("DONT_SHOW_DIALOG", isChecked);
editor.commit();
}
});
return mainView;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
// Do wifi stuff here
break;
case R.id.button2:
// Do cellular stuff here
break;
}
}
}
布局xml wifi_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some information to be displayed to human"
android:id="@+id/textView" android:textSize="30dp"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do now show this dialog again."
android:id="@+id/checkBox"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WiFi"
android:id="@+id/button1" android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CELL"
android:id="@+id/button2"/>
</LinearLayout>
</LinearLayout>
接受的答案是可行的,但它看起来不像正常的“材质设计系统”对话框,这正是我想要的 这里有一个选项,使用自定义视图布局仅保留文本和复选框(带有与材质设计匹配的填充),并允许AlertDialog管理按钮
无位置对话框.xml
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="24dp"
android:paddingRight="12dp">
<TextView
android:id="@+id/no_location_text"
style="?android:attr/textAppearance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="14dp"
android:paddingBottom="6dp"
android:text="@string/main_nolocation"
android:textSize="16sp"
android:autoLink="all"
android:linksClickable="true"></TextView>
<CheckBox
android:id="@+id/location_never_ask_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_never_ask_again" />
</LinearLayout>
看起来是这样的:
以下是Github上提交的完整代码-。您想说什么。。。。。。实际上它正在工作。。。。。Dialog@Er.Arjunsaini我不确定我是否明白?当我启动应用程序时,当前会弹出对话框。我是否根据所选的选项执行任何操作?否,因为我必须说明用户是否选择WiFi only use WiFi,以及他们是否选择data only use data来启动我的OAuth2当单击任何人而不是在其他人上设置enable WiFi或mobie data时,将自定义布局设置为复选框的对话框,并在布局中选中两个复选框OK,获得该部分。在我的dialogfragment中添加一个“不再显示”复选框怎么样?如果启用wifi而不是隐藏wificheckbox,则检查条件,如果启用mobiledata而不是复选框mobile hide Other,则检查是否可以将其添加到我当前拥有的内容中?我想将按钮与复选框一起保留为WiFi和手机数据(4G)。不完全确定是不是。因为我希望用户在WiFi或4G之间进行选择,然后在未启用的情况下启用他们选择的任何一个。如果你能做到这一点,你将成为男人中的上帝。你可以在布局文件中添加任意数量的按钮,并进行相应的处理。我已经编辑了上面的代码。:-)只需注释WifivsDataDialog中的所有复选框代码,或注释行sharedpreferences sharedpreferences sharedPref=getActivity().getPreferences(Context.MODE_PRIVATE);SharedPreferences.Editor=sharedPref.edit();putBoolean(“不显示对话框”,已选中);commit();您需要调用getActivity().getSystemService(Context.WIFI\u服务);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="24dp"
android:paddingRight="12dp">
<TextView
android:id="@+id/no_location_text"
style="?android:attr/textAppearance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="14dp"
android:paddingBottom="6dp"
android:text="@string/main_nolocation"
android:textSize="16sp"
android:autoLink="all"
android:linksClickable="true"></TextView>
<CheckBox
android:id="@+id/location_never_ask_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_never_ask_again" />
</LinearLayout>
private Dialog createNoLocationDialog() {
View view = getActivity().getLayoutInflater().inflate(R.layout.no_location_dialog, null);
CheckBox neverShowDialog = (CheckBox) view.findViewById(R.id.location_never_ask_again);
neverShowDialog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
// Save the preference
PreferenceUtils.saveBoolean(getString(R.string.preference_key_never_show_location_dialog), isChecked);
}
});
Drawable icon = getResources().getDrawable(android.R.drawable.ic_dialog_map);
DrawableCompat.setTint(icon, getResources().getColor(R.color.theme_primary));
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle(R.string.main_nolocation_title)
.setIcon(icon)
.setCancelable(false)
.setView(view)
.setPositiveButton(R.string.rt_yes,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivityForResult(
new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS),
REQUEST_NO_LOCATION);
}
}
)
.setNegativeButton(R.string.rt_no,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Ok, I suppose we can just try looking from where we
// are.
mMapFragment.mController.onLocation();
}
}
);
return builder.create();
}