如何从DialogFragment获取值?

问题描述 投票:-1回答:1

我无法弄清楚如何在onClick方法中使用这些值。我的此类带有onCreateDialog方法,该方法提示用户输入以下User is prompted

这里是代码

public class InfoPrompt extends AppCompatDialogFragment {
private EditText editTextTitle;
private EditText editTextAuthor;
private EditText editTextPageCount;
private EditText editTextDeadline;
//private InfoPromptListener listener;


@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

    View view = inflater.inflate(R.layout.info_prompt, null);

    builder.setView(view);
    builder.setTitle("Add Work");
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            String bookTitle = editTextTitle.getText().toString();
            String bookAuthor = editTextAuthor.getText().toString();

            String pageCount = editTextPageCount.getText().toString();
            Long pageCountLong = Long.parseLong(pageCount);

            String deadline = editTextDeadline.getText().toString();
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            Date deadlineDate = null;
            try {
                deadlineDate = sdf.parse(deadline);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Date currentDate = Calendar.getInstance().getTime();
            Long daysBetweenDates = currentDate.getTime() - deadlineDate.getTime();
            Long pagesPerDay = pageCountLong / daysBetweenDates;
            //listener.applyTexts(bookTitle,bookAuthor,pageCountLong,deadline,daysBetweenDates,pagesPerDay);



        }

    });
    editTextTitle = view.findViewById(R.id.bookTitle);
    editTextAuthor = view.findViewById(R.id.bookAuthor);
    editTextPageCount = view.findViewById(R.id.pageCount);
    editTextDeadline = view.findViewById(R.id.deadline);

    return builder.create();
}

我将不同的字符串设置为等于editText中的输入,但是我不知道如何访问这些变量

当使用这些值使另一个布局膨胀时,我需要在另一个类中使用它们。

我正在按照这些思路思考问题

private void applyTexts(String bookTitle, String bookAuthor, Long pageCountLong, String deadline, Long daysBetweenDates, Long pagesPerDay) {

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.fragment_add_work, null, false);

    TextView bookTitleTV = view.findViewById(R.id.bookTitle);
    TextView bookAuthorTV = view.findViewById(R.id.bookAuthor);
    BubbleSeekBar seekBarBSB = view.findViewById(R.id.seekBar);
    TextView deadlineTV = view.findViewById(R.id.deadline);
    TextView pagesLeftTV = view.findViewById(R.id.pagesLeft);
    TextView todayReadTV = view.findViewById(R.id.todayRead);

    bookTitleTV.setText(bookTitle);
    bookAuthorTV.setText(bookAuthor);
    seekBarBSB.setProgress(pageCountLong);
    deadlineTV.setText(deadline);

    int getProgress = seekBarBSB.getProgress();
    Long pagesLeftLong = pageCountLong-getProgress;
    String pagesLeftString = Long.toString(pagesLeftLong);
    pagesLeftTV.setText(pagesLeftString);

    String ppd = Long.toString(pagesLeftLong/daysBetweenDates);
    todayReadTV.setText(ppd);

    thisLayout.addView(view);

。我对android开发的经验很少,因此很多事情可能都没有意义,但所有帮助都值得赞赏]

java android fragment android-alertdialog
1个回答
0
投票

用于从EditText获取信息:

String userInput = editText.getText().toString().trim()
if (!userInput.isEmpty()) {
    anotherFunctionThatNeedsThisInfo(userInput);
}
private void anotherFunctionThatNeedsThisInfo(String userInput) {
    //Do what you need with the data
    String welcomeText = "Hello, your input is: " + userInput;
}

您需要分别获取每个EditText输入。

我们使用trim()删除用户可能意外添加到输入的空格。

检查用户输入的if语句内是否为空。 !isEpmty()检查它是否为空。

EditText从不返回null,仅当未添加用户输入时,它才返回空。

© www.soinside.com 2019 - 2024. All rights reserved.