使用的EditText在DialogFragment改变的TextView的主要活动? [英] Use EditText in a DialogFragment to change TextView in Main Activity?

查看:631
本文介绍了使用的EditText在DialogFragment改变的TextView的主要活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的工作在Android应用我第一次真正的尝试,只是一个简单的记分员的垒球比赛。我知道了理货分数,奏等,而现在它只是显示家和离开。我想给用户实际的球队的名字进入的机会。为此,我添加了一个自定义AlertDialog与一个EditText弹出,所以当你点击确定它会更新主页/客队的名字。

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.

问题是我一直在谷歌搜索这对于大多数的一周,我还没有发现一种方法来真正做到这一点。我试过标记片段的​​布局XML这样我就可以找到的EditText,但它总是给我一个空引用和崩溃的应用程序。我补充说,presumably观看片段的文字,但一旦改变,并触及TextWatcherOK,什么都没有发生。尝试添加该TextWatch的片段,坠毁我认为,这是大约两个小时前,我已经筋疲力尽了。

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.

说真的,我觉得我需要一种方法来有段找到队名TextView的,并将其更改为EditText上时,我正点击的价值,但我不知道该怎么做。或者,也许我已经看到了,不明白,我只是一直在做这个约两个月。我会发布我的code,但我删除了所有,因为它占用了大部分屏幕房地产,没有工作的东西。任何想法?

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?

编辑:所以我也跟着上定义视图,即发现的EditText presumably的值,低于建议,但打OK只是做了它的TextView设置为空值。我想这是因为EditText上的内容走的对话框被关闭。如果不是这样,这是完全错误的。 查看dialogname = getActivity()getLayoutInflater()膨胀(R.layout.fragment_home,空)。
                        的EditText mEtName =(EditText上)dialogname.findViewById(R.id.homeName);
                        查看mainAct = getActivity()getLayoutInflater()膨胀(R.layout.activity_softball,空)。
                        TextView的oTextView =(TextView中)mainAct.findViewById(R.id.teamOne);
                        。了newName = mEtName.getText()的toString();
                        oTextView.setText(了newName)

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)

推荐答案

我觉得你是在确定的时间做错误的 ID 的EditText 。你需要给它对话的参考。

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

而不是做

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

不喜欢

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

这篇关于使用的EditText在DialogFragment改变的TextView的主要活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆