Android--Fragment與Activity通訊

來源:互聯網
上載者:User

標籤:

package com.example.testfragment;import com.example.testfragment.MainFargment.BackString;import android.app.Activity;import android.app.Fragment;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.Toast;public class MainActivity extends Activity implements BackString{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //靜態載入        //Intent intent = new Intent(this,MainActivity2.class);        //startActivity(intent);                MainFargment fargment = new MainFargment();        //對Fragment通訊傳參數        Bundle bundle = new Bundle();        bundle.putString("name", "Michale");        fargment.setArguments(bundle);        //動態載入 FragmentManager管理者        FragmentManager manager = getFragmentManager();        FragmentTransaction transaction = manager.beginTransaction();        //第一個參數指定目標 動態目標容器        transaction.add(R.id.linearLayout1, fargment, "fargment");        transaction.addToBackStack(null);        transaction.commit();    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }    @Override    public Void WriteString(String code) {        // TODO 自動產生的方法存根        Toast.makeText(this, code, Toast.LENGTH_SHORT).show();        return null;    }}
package com.example.testfragment;import android.app.Activity;import android.os.Bundle;public class MainActivity2 extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO 自動產生的方法存根        super.onCreate(savedInstanceState);        setContentView(R.layout.fragment_main);    }}
package com.example.testfragment;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.EditText;public class MainFargment extends Fragment {    private String name = "hello michale";    public    BackString backString;    public interface BackString    {        public Void WriteString(String code);    }        @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        // TODO 自動產生的方法存根        //return super.onCreateView(inflater, container, savedInstanceState);        View view=inflater.inflate(R.layout.userfragment, container, false);        String strName = getArguments().getString("name").toString();        EditText text = (EditText) view.findViewById(R.id.editText1);        text.setText(strName);        backString.WriteString(name);        return view;    }        @Override    public void onAttach(Activity activity) {        // TODO 自動產生的方法存根        backString = (BackString) activity;        super.onAttach(activity);    }}
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.testfragment.MainActivity" >    <LinearLayout        android:id="@+id/linearLayout1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >    </LinearLayout></RelativeLayout>
<?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" >        <fragment         android:id="@+id/fragment1"        android:name="com.example.testfragment.MainFargment"        android:layout_width="match_parent"        android:layout_height="match_parent"        /></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" >        <EditText        android:id="@+id/editText1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        /></LinearLayout>

 

Android--Fragment與Activity通訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.