Fragment的使用可以讓我們的應用更靈活的適配各種型號的安卓設備,但是對於Fragment和Activity之間的通信,很多朋友應該比較陌生,下面我們就通過一個實例來看一看如何實現。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/rl_fragment"
android:orientation="vertical"
android:layout_height="match_parent" >
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="發送" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_fragment" android:layout_height="wrap_content" android:layout_width="wrap_content" /></LinearLayout>
package com.example.fragementcommunication;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private EditText mMainActivityET;private Button mSendButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);mMainActivityET = (EditText) findViewById(R.id.et_input);mSendButton = (Button) findViewById(R.id.btn_send);mSendButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String text = mMainActivityET.getText().toString();MyFragment myFragment = new MyFragment();Bundle bundle = new Bundle();bundle.putString("input", text);myFragment.setArguments(bundle);// 傳遞stringFragmentManager manager = getFragmentManager();FragmentTransaction transaction = manager.beginTransaction();transaction.add(R.id.rl_fragment, myFragment, "myfragment");transaction.commit();Toast.makeText(MainActivity.this, "向Fragment發送數據" + text, Toast.LENGTH_SHORT).show();}});}}
package com.example.fragementcommunication;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import android.widget.Toast;public class MyFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragmet, null);TextView mFragmentTextView = (TextView) view.findViewById(R.id.tv_fragment);String string = getArguments().getString("input");// 獲取數據;mFragmentTextView.setText(string);Toast.makeText(getActivity(), "成功獲取數據" + string, Toast.LENGTH_SHORT).show();return view;}}
package com.example.fragementcommunication;import com.example.fragementcommunication.MyFragment.MyListener;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity implements MyListener{private EditText mMainActivityET;private Button mSendButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);mMainActivityET = (EditText) findViewById(R.id.et_input);mSendButton = (Button) findViewById(R.id.btn_send);mSendButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String text = mMainActivityET.getText().toString();MyFragment myFragment = new MyFragment();Bundle bundle = new Bundle();bundle.putString("input", text);myFragment.setArguments(bundle);// 傳遞stringFragmentManager manager = getFragmentManager();FragmentTransaction transaction = manager.beginTransaction();transaction.add(R.id.rl_fragment, myFragment, "myfragment");transaction.commit();Toast.makeText(MainActivity.this, "向Fragment發送數據" + text, Toast.LENGTH_SHORT).show();}});}@Overridepublic void callback(String back) {Toast.makeText(MainActivity.this, "獲取到從Fragment中傳來的數據" + back, Toast.LENGTH_SHORT).show();}}
package com.example.fragementcommunication;import android.app.Activity;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import android.widget.Toast;public class MyFragment extends Fragment {public MyListener listener;private String back="已經接收到數據!謝謝";/** * 通過接口回調的方式從Fragment向activity傳值; * @author Administrator * */public interface MyListener{public void callback(String back);}@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);listener=(MyListener) activity;}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragmet, null);TextView mFragmentTextView = (TextView) view.findViewById(R.id.tv_fragment);String string = getArguments().getString("input");// 獲取數據;mFragmentTextView.setText(string);Toast.makeText(getActivity(), "成功獲取數據" + string, Toast.LENGTH_SHORT).show();Toast.makeText(getActivity(), "向Activity傳遞數據" + back, Toast.LENGTH_SHORT).show();listener.callback(back);return view;}}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。