标签:
------------------------------------------------Activity发消息给Fragment-----------------------------------------------
MainActivity
package com.example.handlerway; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; public class MainActivity extends Activity { private Handler mHandler; private EditText edit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edit = (EditText) findViewById(R.id.edit); } /** * 我们需要在Activity中定义一个方法用来设置Handler对象。 */ public void setHandler(Handler handler) { mHandler = handler; } public void button(View view) { HandlerFragment arg = new HandlerFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.layout_fragment, arg); fragmentTransaction.commit(); } /** * 然后我们可以在Activity中发送消息给Fragment中的Hanlder进行交互。 */ public void inter(View view) { Message msg = new Message(); msg.obj = edit.getText().toString(); msg.what = 1; //发送消息 mHandler.sendMessage(msg); } }
package com.example.handlerway; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class HandlerFragment extends Fragment { private TextView text; private MainActivity mActivity; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_handler, container, false); text = (TextView) view.findViewById(R.id.text); return view; } public Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: text.setText((String) msg.obj); break; } }; }; /** * 在Fragment中的回调函数onAttach()中得到Fragment所在Activity,并调用setHandler方法,设置Handler。 * 该Handler在Fragment中定义,用来接收消息与Fragment进行交互。 */ @Override public void onAttach(Activity activity) { super.onAttach(activity); mActivity = (MainActivity) activity; mActivity.setHandler(mHandler); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:padding="10dp" > <FrameLayout android:id="@+id/layout_fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > </FrameLayout> <EditText android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请先添加点击添加Fragment,并输入你要传递的消息" android:lines="5" android:padding="5dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="button" android:padding="10dp" android:text="这个按钮是添加一个Fragment" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="inter" android:padding="10dp" android:text="这个按钮是发送一个消息给Fragment" /> </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#F4A460" android:gravity="center" android:orientation="vertical" android:padding="10dp" > <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:padding="10dp" android:textSize="20dp" /> </LinearLayout>
MainActivity
package com.example.handlerway; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; public class MainActivity extends Activity { private Handler mainActivityHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 1: edit.setText((CharSequence) msg.obj); break; default: break; } }; }; private EditText edit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edit = (EditText) findViewById(R.id.edit); } public void button(View view) { HandlerFragment arg = new HandlerFragment(mainActivityHandler); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.layout_fragment, arg); Bundle args = new Bundle(); args.putString("bbb", "aaa"); arg.setArguments(args); ft.commit(); } }
package com.example.handlerway; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class HandlerFragment extends Fragment implements OnClickListener { private TextView text; private MainActivity mActivity; private EditText editText; private Button button; // 接受handler private Handler fragmentHandler = new Handler(); public HandlerFragment(Handler mainActivityHandler) { super(); this.fragmentHandler = mainActivityHandler; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_handler, container, false); text = (TextView) view.findViewById(R.id.text); editText = (EditText) view.findViewById(R.id.editText); button = (Button) view.findViewById(R.id.button); button.setOnClickListener(this); if (getArguments().getString("bbb").equals("aaa")) { editText.setText("尼玛"); } return view; } @Override public void onAttach(Activity activity) { super.onAttach(activity); mActivity = (MainActivity) activity; } @Override public void onClick(View v) { if (v.getId() == R.id.button) { // 发送消息给activity Message msg = new Message(); msg.obj = editText.getText().toString(); msg.what = 1; fragmentHandler.sendMessage(msg); } } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:padding="10dp" > <FrameLayout android:id="@+id/layout_fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > </FrameLayout> <EditText android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请先添加点击添加Fragment,并输入你要传递的消息" android:lines="5" android:padding="5dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="button" android:padding="10dp" android:text="Click to add a fragment" /> </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:padding="10dp" > <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" /> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > </EditText> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
标签:
原文地址:http://blog.csdn.net/u013210620/article/details/46522271