1. 程式人生 > >Fragment學習之使用介面回撥的方式實現Fragment與Activity通訊

Fragment學習之使用介面回撥的方式實現Fragment與Activity通訊

Fragment與Fragment之間可以進行資訊傳遞,同樣,Fragment與Activity也可以進行資訊的傳遞。

下面是一個演示在Activity中獲取來自Fragment的資訊,使用介面回撥的方法在Activity中接收資訊

MainActivity.java:

public class MainActivity extends FragmentActivity {

	private FragmentManager manager;
	private FragmentTransaction transaction;
	private Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		/* 獲取對應的控制元件 */
		button = (Button) this.findViewById(R.id.button);
		/* 獲取manager */
		manager = this.getSupportFragmentManager();
		/* 建立事物 */
		transaction = manager.beginTransaction();

		/* 建立LeftFragment(在內部類中使用到了,所以要用final) */
		final LeftFragment leftFragment = new LeftFragment();
		/* 把Fragment新增到對應的位置 */
		transaction.add(R.id.left, leftFragment, "left");
		/* 提交事物 */
		transaction.commit();

		/* 設定按鈕的監聽事件 */
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				/* 使用介面回撥的方法獲取資料 */
				leftFragment.getEditText(new CallBack() {

					@Override
					public void getResult(String result) {
						/*列印資訊*/
						Toast.makeText(MainActivity.this, "-->>" + result, 1).show();
					}
				});

			}
		});

	}

}

LeftFragment.java程式碼:
public class LeftFragment extends Fragment {

	private Button button;
	private EditText editText;

	public LeftFragment() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		/*動態載入佈局*/
		View view = inflater.inflate(R.layout.left, null);
		/*從動態佈局中獲取對應的控制元件*/
		editText = (EditText) view.findViewById(R.id.editText1);
		return view;
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	/*介面回撥*/
	public void getEditText(CallBack callBack){
		/*獲取文字框的資訊*/
		String msg = editText.getText().toString();
		callBack.getResult(msg);
	}
	
	/*介面*/
	public interface CallBack{
		/*定義一個獲取資訊的方法*/
		public void getResult(String result);
	}
}

activity_main.xml:
<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:orientation="horizontal"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/left"
        android:layout_width="224dp"
        android:layout_height="match_parent"
        android:background="#CCCCCC"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="獲得Fragment的值" />
    </LinearLayout>

</LinearLayout>

left.xml:
<?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" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

</LinearLayout>

demo演示效果: