Fragment Intermediate(I):Getting input from edittext, set text in textview of a fragment

≯℡__Kan透↙ 提交于 2019-12-02 04:01:40
Raghunandan

There are two ways you can add a fragment to the activity layout:

  1. Declare the fragment inside the activity's layout file.

  2. Programmatically add the fragment to an existing ViewGroup.

Both methods are mentioned in the docs

http://developer.android.com/guide/components/fragments.html

If you want add the fragment to a container you need to use a ViewGroup in xml. Generally FrameLayout is used. So have the below in xml

 <FrameLayout
        android:id="@+id/fragment_place"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

The Activity code is fine. On Button click you replace the appropriate fragment in the container.

In your Fragment onCreateView you inflate the fragment layout and use the view object to initialize views of that layout. But you inflate activity_main.xml which is not required.

Quoting docs

Specifically, the fragment can access the Activity instance with getActivity() and easily perform tasks such as find a view in the activity layout

So for EditText you can use

EditText easy = (EditText)getActivity().findViewById(R.id.easy);

Or Initialize EditText in Activity on Button click get the value from EditTextand then you can pass the value of EditText from Activity to Fragment

Send data from activity to fragment in android

show me the onclick method, and the container u have fragment one in... its one of two things,

either you are programmatically adding a view which already exists, or you are tryin to update the ui without a handler.... handlers can be worked around in fragments, so im assuming its the first one....

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