Android學習筆記之fragment的通訊

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   sp   

1.布局檔案

mian.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     android:id="@+id/linearLayout" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6  7     <EditText 8         android:id="@+id/editText1" 9         android:layout_width="match_parent"10         android:layout_height="wrap_content" />11 12     <Button13         android:id="@+id/button1"14         android:layout_width="match_parent"15         android:layout_height="wrap_content" />16 17 </LinearLayout>
View Code

fragment.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     android:layout_width="match_parent" 3     android:layout_height="match_parent" 4     android:orientation="vertical" > 5      6     <TextView  7         android:id="@+id/textView1" 8         android:layout_width="match_parent" 9         android:layout_height="wrap_content"/>10     11 12 </LinearLayout>
View Code

2.java代碼

MainActivity.class

 1 public class MainActivity extends ActionBarActivity  implements MyListener{ 2  3     private Button b1; 4     private EditText et1; 5  6     @Override 7     protected void onCreate(Bundle savedInstanceState) { 8  9         super.onCreate(savedInstanceState);10         setContentView(R.layout.main);11 12         et1 = (EditText) findViewById( R.id.editText1);13         b1 = (Button) findViewById(R.id.button1);14         b1.setOnClickListener(new OnClickListener() {15 16             @Override17             public void onClick(View v) {18                 // TODO Auto-generated method stub19                 String text = et1.getText().toString();20                 Bundle bundle = new Bundle();21                 Myfragment myfragment = new Myfragment();22                 bundle.putString("name", text);23                 myfragment.setArguments(bundle);24                 FragmentManager fragmentManager = getFragmentManager();25                 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();26                 fragmentTransaction.add(R.id.linearLayout, myfragment, "myfragment");27                 fragmentTransaction.addToBackStack(null);28                 fragmentTransaction.commit();29                 Toast.makeText(MainActivity.this, "向fragment發送資料"+text, Toast.LENGTH_SHORT).show();30 31             }32         });33     }34 35     @Override36     public void thank(String code) {37         // TODO Auto-generated method stub38         Toast.makeText(MainActivity.this, "成功接收"+code+"客氣", Toast.LENGTH_SHORT).show();39     }40 41 42 }
View Code

Myfragment

 1 public class Myfragment extends Fragment{ 2      3     private TextView tv1; 4     private String code = "Thanks"; 5     private MyListener listener; 6      7     public interface MyListener{ 8          9         public void thank(String code);10             11     }12     13     @Override14     public void onAttach(Activity activity) {15         // TODO Auto-generated method stub16         listener = (MyListener) activity;17         super.onAttach(activity);18     }19     20     @Override21     public View onCreateView(LayoutInflater inflater, ViewGroup container,22             Bundle savedInstanceState) {23         // TODO Auto-generated method stub24         View view = inflater.inflate(R.layout.fragment, container,false);25         tv1 = (TextView) view.findViewById(R.id.textView1);26         String text = getArguments().get("name")+"";27         tv1.setText(text);28         Toast.makeText(getActivity(), "已經成功接收"+text, Toast.LENGTH_SHORT).show();29         Toast.makeText(getActivity(), "向Activity發送"+text, Toast.LENGTH_SHORT).show();30         listener.thank(code);31         return view;32     }33 34 }
View Code

 

Android學習筆記之fragment的通訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.