无法从片段

时间:2017-10-18 23:31:41

标签: android android-edittext fragment alertdialog

我希望用两个EditTexts创建一个AlertDialog。 AlertDialog存在于片段内。

onCreateView我得到了片段布局以及我用于AlertDialog的布局的参考。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.daily_tasks, container, false);
    View dialogView = inflater.inflate(R.layout.daily_dialog, container, false);

    floatDailyButton = (FloatingActionButton) dialogView.findViewById(R.id.floatDailyButton);
    taskTitle = (EditText) dialogView.findViewById(R.id.taskTitleEditText);
    taskDescription = (EditText) dialogView.findViewById(R.id.taskDescriptionEditText);

    floatDailyButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("Tapped", "tapped");
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setView(R.layout.daily_dialog);
            builder.setPositiveButton("Save Tasks", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    if (getText() == true) {
                        saveData();
                    } else {
                        Toast.makeText(getContext(), "Set a title", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            builder.show();
        }
    });
    return view;

当用户按下对话框中的“肯定”按钮时,我想从EditTexts中提取内容:

public boolean getText() {
    try {
        titleText = taskTitle.getText().toString();
        Toast.makeText(getActivity(), titleText, Toast.LENGTH_SHORT).show();
        Log.i("title text", titleText);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    try {
        descriptionText = taskDescription.getText().toString();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

然而,无论这些EditTexts中的值是什么,当我点击肯定按钮时,AlertDialog关闭,EditTexts中的值为null。

这里有什么问题?

0 个答案:

没有答案