【问题标题】:Save state from checkbox in custom Dialog从自定义对话框中的复选框保存状态
【发布时间】:2020-10-06 02:11:48
【问题描述】:

伙计们,我已经创建了一个自定义对话框,其中包含一个编辑文本和一个带有此 XML 代码的复选框

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dialog_rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity"
android:background="#000000"
>

<TextView
    android:id="@+id/dialog_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:padding="5dp"
    android:text="Velocidade de Trabalho"
    android:textColor="#ffffff"
    android:textSize="32dp" />

<EditText
    android:id="@+id/et_name"
    android:layout_width="946dp"
    android:layout_height="105dp"
    android:layout_below="@id/dialog_title"
    android:fontFamily="@font/digital"
    android:hint="Digite a Taxa Desejada"
    android:inputType="number"
    android:maxLength="3"
    android:textColor="#ffffff"
    android:textSize="50dp"
    tools:ignore="HardcodedText" />

<CheckBox
    android:id="@+id/checkbox"
    style="?android:attr/textAppearanceMedium"
    android:layout_width="1000dp"
    android:layout_height="71dp"
    android:text="Habilitar GPS"
    android:textColor="#ffffff"
    android:textSize="32dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="172dp" />

<Button
    android:id="@+id/dialog_negative_btn"
    android:layout_width="220dp"
    android:layout_height="110dp"
    android:layout_below="@+id/et_name"
    android:layout_centerVertical="true"
    android:layout_marginTop="92dp"
    android:layout_marginEnd="10dp"
    android:layout_marginRight="10dp"
    android:layout_toStartOf="@+id/dialog_positive_btn"
    android:layout_toLeftOf="@id/dialog_positive_btn"
    android:background="#ffffff"
    android:text="CANCELAR"
    android:textSize="22dp" />

<Button
    android:id="@+id/dialog_positive_btn"
    android:layout_width="220dp"
    android:layout_height="110dp"
    android:layout_below="@id/et_name"
    android:layout_alignEnd="@+id/et_name"
    android:layout_centerVertical="true"
    android:layout_marginTop="90dp"
    android:layout_marginEnd="1dp"
    android:background="#ffffff"
    android:text="CONFIRMAR"
    android:textSize="22dp" />

在我的片段中,我创建了一个显示对话框的方法,该对话框在单击时显示,在显示该框后,我选中复选框中的选项并使用肯定按钮确认,但再次单击以显示我的对话框时复选框状态未保存如何保存此状态?

这是我在片段中的方法

 private void showdialogGPS (){

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.dialog_gps,null);

    // Specify alert dialog is not cancelable/not ignorable
    builder.setCancelable(false);

    // Set the custom layout as alert dialog view
    builder.setView(dialogView);

    // Get the custom alert dialog view widgets reference
    Button btn_positive =  dialogView.findViewById(R.id.dialog_positive_btn);
    Button btn_negative =  dialogView.findViewById(R.id.dialog_negative_btn);
    CheckBox btn_Habilita = dialogView.findViewById(R.id.checkbox);
    final EditText et_name = dialogView.findViewById(R.id.et_name);

    btn_Habilita.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            if (isChecked){
                  habilitagps = true;
       }
            else {
                habilitagps = false;
            }
                        }
    });

    // Create the alert dialog
    final AlertDialog dialog = builder.create();

    btn_positive.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Dismiss the alert dialog
            dialog.cancel();
            String name = et_name.getText().toString();
            Toast.makeText(getActivity(),
                "Velocidade de Trabalho : " + name +  ("\u2705"), Toast.LENGTH_LONG).show();
            // Say hello to the submitter
            Txaplicacao.setText(name);
             if (habilitagps){
                 Toast.makeText(getActivity(),
                     "GPS Habilitado " +  ("\u2705"), Toast.LENGTH_LONG).show();
             }else{
                 Toast.makeText(getActivity(),
                     "GPS Desabilitado " +  ("\u2705"), Toast.LENGTH_LONG).show();
             }
        }
    });

    // Set negative/no button click listener
    btn_negative.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Dismiss/cancel the alert dialog
            //dialog.cancel();
            dialog.dismiss();
            Toast.makeText(getActivity(),
                "Valor Não Alterado" + ("\u274c"), Toast.LENGTH_LONG).show();
        }
    });

    dialog.show();
}

【问题讨论】:

    标签: java android android-studio checkbox android-alertdialog


    【解决方案1】:

    只需使用 Android Studio 中的 SharedPreferences:

    【讨论】:

    • 对,但是我必须在我的代码中将这些值保存在哪里,当我使用我的 showdialog 时我应该在哪里恢复,复选框将被选中,你能解释一下吗?
    • 在你的构建器中添加这样的方法builder.setPositiveButton("OK", (dialog, id) -&gt; {...then save here what to save is});
    • 我将其保存,但是当我再次单击并显示对话框时,我无法恢复该值,因此它已经被检查过,它将再次为空
    • 我明白了,感谢帮助,我通过sharedpreferences保存了值,然后恢复并将参数传递给btn_Habilita.setChecked(habilitagps);这样,当我再次单击时,保留复选框的状态。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 2012-04-18
    • 2011-08-23
    • 1970-01-01
    相关资源
    最近更新 更多