[英]Android fragments activity
I have a small problem. 我有一个小问题。 in my activity i have a button and in my fragment i have textboxes.
在我的活动中我有一个按钮,在我的片段中我有文本框。
Now when i click on the button i want see a dialog with the information of the textboxes.Below you can see my dialog that stands in my activity 现在,当我点击按钮时,我想看到一个包含文本框信息的对话框。您可以在我的活动中看到我的对话框
public void onFragmentInteraction(Person uri) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("test");
builder.show();
}
My actionlistener of my button is in my activity and my textboxes are in my fragment. 我的按钮的actionlistener在我的活动中,我的文本框在我的片段中。 I have tried this with a bundle but this didn't work.
我用捆绑试过这个但是这个没用。
Someone can help me with this ? 有人可以帮我这个吗?
If you really must keep your button and textviews in seperate activity/components then you'll need to allow some communication between the two. 如果您真的必须将按钮和文本视图保留在单独的活动/组件中,那么您需要允许两者之间进行一些通信。
here's a nice tutorial on how to do this - https://developer.android.com/training/basics/fragments/communicating.html 这是一个很好的教程,如何做到这一点 - https://developer.android.com/training/basics/fragments/communicating.html
A really rough way to achieve what you want is to pass text changes from your textview and store them in your activity. 实现目标的一种非常粗略的方法是从文本视图中传递文本更改并将其存储在您的活动中。
something like - 就像是 -
textView.addTextChangedListener(new TextWatcher() {
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override public void afterTextChanged(Editable s) {
((ActivityName)getActivity()).setSomeText(s.toString());
}
});
in the activity you would then create a matching public method 然后,您将在活动中创建匹配的公共方法
public void setSomeText(String someText) {
this.someText = someText;
}
you can then access "someText" in the activity. 然后,您可以在活动中访问“someText”。
This should be done through an interface 这应该通过一个接口完成
您应该将侦听器放在片段中,并以编程方式在Java中而不是在XML中进行设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.