Use EditText in a DialogFragment to change TextView in Main Activity?

天涯浪子 提交于 2019-12-25 16:55:44

问题


So I'm working on my first real attempt at an Android app, just a simple scorekeeper for softball games. I've got it tallying scores, outs, etc, and right now it just displays "Home" and "Away." I'd like to give users the chance to enter in actual team names. To this effect, I added a custom AlertDialog that pops up with an EditText, so when you hit "OK" it'll update the Home/Away team name.

The problem is I've been Googling this for most of the week and I've not found a single way to actually do this. I've tried tagging the fragment's layout XML so I can find the EditText, but it always gives me a null reference and crashes the app. I added a TextWatcher that presumably watched the fragment's text, but once changed and hit "OK," nothing happened. Tried adding the TextWatch to the fragment, that crashed I think, it was about two hours ago and I'm exhausted.

Really, I think I need a way to have the fragment find the TextView with the team name and change it to the value of the EditText when I positive click, but I don't know how to do that. Or maybe I've seen it and don't understand it, I've only been doing this about two months. I'd post my code, but I deleted out all the stuff that didn't work because it was taking up most of the screen real estate. Any ideas?

EDIT: So I followed advice below on defining views, that found the value of the EditText presumably, but hitting "OK" just made it set the TextView to a blank value. I think this is because the EditText's contents went away as the dialog was closed. Either that or this is all wrong. View dialogname = getActivity().getLayoutInflater().inflate(R.layout.fragment_home, null); EditText mEtName = (EditText) dialogname.findViewById(R.id.homeName); View mainAct = getActivity().getLayoutInflater().inflate(R.layout.activity_softball, null); TextView oTextView = (TextView) mainAct.findViewById(R.id.teamOne); newName = mEtName.getText().toString(); oTextView.setText(newName)


回答1:


I think you are doing wrong at time of defining id for EditText. You need to give it dialog reference.

rather than doing

 Edittext edittext = (EditText) findViewById(R.id.editText);

do like

 Edittext edittext = (EditText) dialog.findViewById(R.id.editText);



回答2:


Been through this issue a while ago. I created my own class which extended DialogFragment. When I tried to initialize EditText which was in the dialog, I got null like

mEtName = (EditText)dialog.findViewById(R.id.editText);

So, I initialized it in this way and it worked:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    ...
    View dialogName = getActivity().getLayoutInflater().inflate(R.layout.dialog_name, null);
    mEtName = (EditText) dialogName.findViewById(R.id.dialog_etxt_name);
    }

Where R.layout.dialog_name is my custom dialog layout.



来源:https://stackoverflow.com/questions/32730992/use-edittext-in-a-dialogfragment-to-change-textview-in-main-activity

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