簡體   English   中英

如何獲取我們自定義的 AlertDialog 的值?

[英]How to get the value of our custom AlertDialog?

我創建了一個自定義 AlertDialog,我想保存用戶設置的值。 但是我無法訪問它們,因為Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

我在 HomeFragment 中,我創建了 AlertDialog 並設置了布局。 我是這樣做的:

  public class HomeFragment extends Fragment implements CustomDialog.CustomDialogListener {

    private HomeViewModel homeViewModel;
    private View root;

    public View onCreateView(@NonNull final LayoutInflater inflater,
                             final ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                ViewModelProviders.of(this).get(HomeViewModel.class);
        root = inflater.inflate(R.layout.fragment_home, container, false);
        final TextView textView = root.findViewById(R.id.text_home);
        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });

        Button btn_add_apero = (Button) root.findViewById(R.id.btn_add_apero);
        btn_add_apero.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(root.getContext());
                builder.setTitle("Super un nouvel apéro !");
                final EditText name_apero = (EditText)root.findViewById(R.id.edit_apero);
                final EditText date_apero = (EditText)root.findViewById(R.id.edit_date);

                builder.setView(inflater.inflate(R.layout.layout_dialog, null))
                        // Add action buttons
                        .setPositiveButton(R.string.text_add, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                LaperoDatabase db = Room.databaseBuilder(root.getContext(),
                                        LaperoDatabase.class, "lapero_db").allowMainThreadQueries().build();

                                AperoDao dbApero = db.getAperoDao();
                                Apero new_apero = new Apero(name_apero.getText().toString(), date_apero.getText().toString());
                                dbApero.insert(new_apero);
                            }
                        })
                        .setNegativeButton(R.string.text_cancel, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
                builder.show();
            }
        });
        return root;
    }

在我的layout_dialog.xml我設置了 2 EditText :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <EditText
        android:id="@+id/edit_apero"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/edit_apero" />

    <EditText
        android:id="@+id/edit_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/edit_apero"
        android:layout_alignParentStart="true"
        android:hint="@string/edit_date"
        android:layout_alignParentLeft="true" />

</RelativeLayout>  

我怎樣才能設法獲得這兩個字段中的值?

我注意到的第一件事是您使用root來查找對話框視圖,但root指向您的片段布局。 root = inflater.inflate(R.layout.fragment_home, container, false);

在引用它的資源 id 之前首先膨脹你的對話框布局。

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View inflater = getLayoutInflater().inflate(R.layout.layout_dialog, null);
builder.setView(inflater);

然后將root更改為inflater以便您從對話框布局中引用視圖

 final EditText name_apero = (EditText)inflater.findViewById(R.id.edit_apero);
 final EditText date_apero = (EditText)inflater.findViewById(R.id.edit_date);

完整代碼

Button btn_add_apero = (Button) root.findViewById(R.id.btn_add_apero);
btn_add_apero.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            View inflater = getLayoutInflater().inflate(R.layout.layout_dialog, null);
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setView(inflater);
            builder.setTitle("Super un nouvel apéro !");

            final EditText name_apero = (EditText)inflater.findViewById(R.id.edit_apero);
            final EditText date_apero = (EditText)inflater.findViewById(R.id.edit_date);
                        // Add action buttons
                     builder.setPositiveButton(R.string.text_add, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                LaperoDatabase db = Room.databaseBuilder(root.getContext(),
                                        LaperoDatabase.class, "lapero_db").allowMainThreadQueries().build();

                                AperoDao dbApero = db.getAperoDao();
                                Apero new_apero = new Apero(name_apero.getText().toString(), date_apero.getText().toString());
                                dbApero.insert(new_apero);
                            }
                        })
                     builder.setNegativeButton(R.string.text_cancel, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
                builder.show();
            }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM