How do I connect a button to a Dialog that will edit a TextView, within a FrameLayout, with a users input?

血红的双手。 提交于 2020-01-06 09:54:06

问题


I don't understand DialogFragment at all. How to create one, how to get the user input out of it, and set it into a TextView.

I would like for the TITLE button, when clicked, to bring up a DialogFragment asking the user to enter the title of their Mood Board. The user enters a title. When they click the PostiveButton "Done", the user's title is set into the top left frame of the mood board, which has a TextView with a hint.

Please! Ask questions, because I don't really understand the dialog setup.

Here is a picture of my main_layout, in my MainActivity. Every element has an "@+id/".


回答1:


The solution you are looking for is a callback:

  1. Create an interface with a method to use as a callback
  2. Implements the interface on the activity
  3. Create the dialog fragment and in onAttach get the interface
  4. Show the dialog fragment on the activity
  5. On dismiss the dialog fragment pass the text using the instance of the interface
interface Callback {
    updateText(String text)
}
class CoolActivity... implements Callback

onCreate {
    //find your views
    showDialogBtn.setOnClickListener(...
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment prev = getSupportFragmentManager().findFragmentByTag("yourTag");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);

        DialogFragment dialogFragment = ExampleDialogFragment.newInstance();
        dialogFragment.show(ft, "yourTag");
     )
}


@Override
updateText(String text) {
    youtView.setText(text)
}
class CoolDialogFragment extend DialogFragment {

     private Callback callback;


     @Override
      void onAttach(Context context) {
          callback = (Callback) context
      }

      @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dialog_fragment_example, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
         //find the views in the dialog fragment
         closeBtn.clickListener(...
               callback.updateText(edittext.getText().toString())
               dismiss()
         )
    }
}

Here is a gist of a dialog fragment https://gist.github.com/cutiko/7e307efcf7492ea52ef05cd90f9e3203

The problem is you want to connect a dialog fragment with a another component, and you want to do it straigth forward. This is not considered a good practice because yiu create 2 componentes higly attached, so the best would be to use data persistence and some form of react programming




回答2:


You can make your mood board title textview static then call it to the alertdialog with edittext to set it text (setText) like this.

   final EditText edittext = new EditText(MainActivity.this);

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setMessage("Input Title")
                .setView(edittext)
                .setCancelable(false)
                .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        YourCustomDialog.your_title_textviewMoodboard.setText(edittext.getText().toString());

                    }
                })
                .setNegativeButton("Back", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                    }
                });
        AlertDialog alert = builder.create();
        alert.show();

in your custom dialog. declare your textview static globally

public static TextView your_title_textviewMoodboard;


来源:https://stackoverflow.com/questions/58244981/how-do-i-connect-a-button-to-a-dialog-that-will-edit-a-textview-within-a-framel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!