編輯:關於Android編程
08-21 04:47:16.169 1164-1164/com.example.fragment I/MainActivity: MainActivity->onCreate() 08-21 04:47:16.173 1164-1164/com.example.fragment D/fragment1: fragment1->onAttach() 08-21 04:47:16.173 1164-1164/com.example.fragment D/fragment1: fragment1->onCreate() 08-21 04:47:16.173 1164-1164/com.example.fragment D/fragment1: fragment1->onCreateView() 08-21 04:47:16.173 1164-1164/com.example.fragment I/MainActivity: MainActivity->onStart() 08-21 04:47:16.173 1164-1164/com.example.fragment D/fragment1: fragment1->onActivityCreated() 08-21 04:47:16.173 1164-1164/com.example.fragment D/fragment1: fragment1->onStart() 08-21 04:47:16.185 1164-1164/com.example.fragment I/MainActivity: MainActivity->onResume() 08-21 04:47:16.185 1164-1164/com.example.fragment D/fragment1: fragment1->onResume() 08-21 04:47:22.177 1164-1164/com.example.fragment I/MainActivity: MainActivity->onPause() 08-21 04:47:22.177 1164-1164/com.example.fragment D/fragment1: fragment1->onPause() 08-21 04:47:22.777 1164-1164/com.example.fragment I/MainActivity: MainActivity->onStop() 08-21 04:47:22.777 1164-1164/com.example.fragment D/fragment1: fragment1->onStop() 08-21 04:47:26.149 1164-1164/com.example.fragment I/MainActivity: MainActivity->onRestart() 08-21 04:47:26.149 1164-1164/com.example.fragment I/MainActivity: MainActivity->onStart() 08-21 04:47:26.149 1164-1164/com.example.fragment D/fragment1: fragment1->onStart() 08-21 04:47:26.149 1164-1164/com.example.fragment I/MainActivity: MainActivity->onResume() 08-21 04:47:26.149 1164-1164/com.example.fragment D/fragment1: fragment1->onResume() 08-21 04:47:28.401 1164-1164/com.example.fragment I/MainActivity: MainActivity->onPause() 08-21 04:47:28.401 1164-1164/com.example.fragment D/fragment1: fragment1->onPause() 08-21 04:47:28.877 1164-1164/com.example.fragment I/MainActivity: MainActivity->onStop() 08-21 04:47:28.877 1164-1164/com.example.fragment D/fragment1: fragment1->onStop() 08-21 04:47:28.877 1164-1164/com.example.fragment I/MainActivity: MainActivity->onDestroy() 08-21 04:47:28.877 1164-1164/com.example.fragment D/fragment1: fragment1->onDestroyView() 08-21 04:47:28.877 1164-1164/com.example.fragment D/fragment1: fragment1->onDestroy() 08-21 04:47:28.877 1164-1164/com.example.fragment D/fragment1: fragment1->onDetach()
package com.example.fragment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; public class MainActivity extends AppCompatActivity { private final String Tag="MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(Tag, "MainActivity->onCreate()"); setContentView(R.layout.activity_main); } @Override protected void onStart() { Log.i(Tag, "MainActivity->onStart()"); super.onStart(); } @Override protected void onRestart() { Log.i(Tag, "MainActivity->onRestart()"); super.onRestart(); } @Override protected void onResume() { Log.i(Tag, "MainActivity->onResume()"); super.onResume(); } @Override protected void onPause() { Log.i(Tag, "MainActivity->onPause()"); super.onPause(); } @Override protected void onStop() { Log.i(Tag, "MainActivity->onStop()"); super.onStop(); } @Override protected void onDestroy() { Log.i(Tag, "MainActivity->onDestroy()"); super.onDestroy(); } }
package com.example.fragment; import android.app.Activity; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by elimy on 2016-08-21. */ public class Fragment1 extends Fragment { private final String Tag ="fragment1"; /* * 調用時間:繪制界面的時候回調,返回Fragment view * 參數: * inflater布局加載器用於從xml加載fragment * container:指示fragment存放的parent view * savedInstanceState:有人解釋說是,如果是true,fragment xml布局會加入到parent view * 的ViewGroup中,則根元素就是container,如果為false,fragment xml會作為單獨的view * 返回,根元素為fragment xml的根元素,沒有深入了解就不裝逼了,大家自己去學習學習大神們的吧 * */ @Nullable public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { Log.d(Tag,"fragment1->onCreateView()"); return inflater.inflate(R.layout.fragment,container,false); } /* * 調用時間:在Fragment綁定到Activity時調用,只調用一次 * activity:及綁定的activity * */ @Override public void onAttach(Activity activity) { Log.d(Tag,"fragment1->onAttach()"); super.onAttach(activity); } /* * 調用時間:在Fragment創建時調用,只調用一次 * */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(Tag, "fragment1->onCreate()"); } /* * 調用時間:在Fragment所在的Activity創建完成後調用 * */ @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { Log.d(Tag,"fragment1->onActivityCreated()"); super.onActivityCreated(savedInstanceState); } /* * 調用時間:Fragment啟動時回調 * */ @Override public void onStart() { Log.d(Tag,"fragment1->onStart()"); super.onStart(); } /* * 調用時間:Fragment獲取焦點是回調 * */ @Override public void onResume() { Log.d(Tag,"fragment1->onResume()"); super.onResume(); } /* *調用時間:Fragment暫停,並進入後台未獲取焦點時回調 * */ @Override public void onPause() { Log.d(Tag,"fragment1->onPause()"); super.onPause(); } /* * 調用時間:Fragment停止時回調 * */ @Override public void onStop() { Log.d(Tag,"fragment1->onStop()"); super.onStop(); } /* * 調用時間: 銷毀 Fragement 繪制的 View 組件時回調 * */ @Override public void onDestroyView() { super.onDestroyView(); Log.d(Tag, "fragment1->onDestroyView()"); } /* * 調用時間: 銷毀 Fragement 回調 * */ @Override public void onDestroy() { Log.d(Tag,"fragment1->onDestroy()"); super.onDestroy(); } /* * 調用時間: Fragement與Activity解綁時回調 * */ @Override public void onDetach() { Log.d(Tag,"fragment1->onDetach()"); super.onDetach(); } }
package com.example.fragment; //注意這裡是導入的android.app包下的Fragment import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by elimy on 2016-08-21. */ public class Fragment_girl1 extends Fragment { public Fragment_girl1() { super(); } @Nullable public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_girl1,container,false); } }
package com.example.fragment; //注意這裡導入的是android.support.v4.app包下的Fragment import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by elimy on 2016-08-21. */ public class Fragment_girl2 extends Fragment { public Fragment_girl2() { super(); } @Nullable public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_girl2,container,false); } }
package com.example.fragment; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.transition.Transition; import android.view.View; import android.widget.Button; /** * Created by elimy on 2016-08-21. */ public class ThreeActivity extends AppCompatActivity { private Button add,v4_add; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_three); add= (Button) findViewById(R.id.add); v4_add= (Button) findViewById(R.id.v4_add); add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //記得這裡面的Fragment也是非V4包下的哦 Fragment_girl1 girl1 = new Fragment_girl1(); //實例化非V4包下的FragmentManager android.app.FragmentManager fragmentManager = getFragmentManager(); //實例化非V4包下的FragmentTransaction android.app.FragmentTransaction transaction = fragmentManager.beginTransaction(); //替換原來的布局 transaction.replace(R.id.activity_three_layout,girl1); //提交事務 transaction.commit(); } }); v4_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //實例化V4包下的FragmentManager FragmentManager fragmentManager =getSupportFragmentManager(); //實例化FragmentTransaction對象 FragmentTransaction v4_transaction = fragmentManager.beginTransaction(); //實例化自定義的碎片 Fragment_girl2 girl2 = new Fragment_girl2(); //替換掉activity的根布局 v4_transaction.replace(R.id.activity_three_layout,girl2); //提交事務 v4_transaction.commit(); } }); } }
package com.example.fragmentdemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
package com.example.fragmentdemo; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; /** * Created by elimy on 2016-08-22. */ public class GirlOneFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_one, container, false); } /* * 調用時間:在Fragment所在的Activity創建完成後調用 * */ @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { //通過Fragment的getActivity()方法獲取到宿主Activity在根據宿主Activity的findViewById()尋找相應的空間 Button add = (Button) getActivity().findViewById(R.id.send_one); final EditText edit1= (EditText) getActivity().findViewById(R.id.edit); final TextView girl1_say= (TextView) getActivity().findViewById(R.id.girl1_say); //為girl1的發送按鈕添加監聽 add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //獲取輸入框的文字 String content = edit1.getText().toString().trim(); //設置到girl2 fragment顯示 girl1_say.setText(content); } }); super.onActivityCreated(savedInstanceState); } }
package com.example.fragmentdemo; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; /** * Created by elimy on 2016-08-22. */ public class GirlTwoFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_two, container, false); } /* * 調用時間:在Fragment所在的Activity創建完成後調用 * */ @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { //通過Fragment的getActivity()方法獲取到宿主Activity在根據宿主Activity的findViewById()尋找相應的空間 Button add = (Button) getActivity().findViewById(R.id.send_two); final EditText edit2= (EditText) getActivity().findViewById(R.id.edit_two); final TextView girl2_say= (TextView) getActivity().findViewById(R.id.girl2_say); //為girl2的發送按鈕添加監聽 add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //獲取輸入框的文字 String content = edit2.getText().toString().trim(); //設置到girl1 fragment顯示 girl2_say.setText(content); } }); super.onActivityCreated(savedInstanceState); } }
6.Fragment與Activity的通信
6.1 簡單方法//通過findFragmentById()實例化GirlOneFragment,參數來著GirlOneFragment對應的主布局xml中的fragment id值 GirlOneFragment girlOneFragment1= (GirlOneFragment) getSupportFragmentManager().findFragmentById(R.id.girlOneFg); //通過實例化調用GirlOneFragment的自定義GetString()方法 girlOneFragment1.GetString(); //通過findFragmentByTag()實例化GirlOneFragment GirlOneFragment girlOneFragment2 = getSupportFragmentManager().findFragmentByTag() //直接new GirlOneFragment girlOneFragment = new GirlOneFragment(); //調用GirlOneFragment自定義方法GetString() girlOneFragment.GetString();
package com.example.fragmentdemo2; import android.app.Activity; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by elimy on 2016-08-23. */ public class InterfaceContactFragment extends Fragment { //定義接口成員變量 public ShowMessage showMessageCallback; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_contact, container, false); } @Override public void onAttach(Activity activity) { super.onAttach(activity); //因為宿主Activity繼承了ShowMessage接口,所以可以強制類型轉換 showMessageCallback = (ShowMessage) activity; showMessageCallback.showMessage(this,"Hello activity,I am InterfaceContactFragment!"); } //定義通信接口 public interface ShowMessage{ public void showMessage(Fragment fragment, String info); } }
package com.example.fragmentdemo2; import android.os.Bundle; import android.os.PersistableBundle; import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; import android.util.Log; /** * Created by elimy on 2016-08-23. */ public class MainActivity extends AppCompatActivity implements InterfaceContactFragment.ShowMessage{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* //實例化自定義Fragment InterfaceContactFragment interfaceContactFragment= new InterfaceContactFragment(); //實例化FragmentManager FragmentManager fragmentManager=getSupportFragmentManager(); //實例化FragmentTransaction FragmentTransaction transaction = fragmentManager.beginTransaction(); //通過事務replace()方法替換掉原來的布局 transaction.replace(R.id.fragment_linear,interfaceContactFragment); //提交事務 transaction.commit();*/ } /* 繼承InterfaceContactFragment的內部ShowMessage接口並實現showMessage方法 *這裡參數由fragment Attach到activity時由實例化的接口成員變量調用回調方法傳遞過來 */ @Override public void showMessage(Fragment fragment, String info) { Log.d("showMessage",info+fragment.getId()); } }
(1)InterfaceContactFragment.java中注冊Intent並發送廣播
package com.example.fragmentdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by elimy on 2016-08-23. */ public class InterfaceContactFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_contact, container, false); } @Override public void onAttach(Activity activity) { super.onAttach(activity); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); /* * Intent intent1 = new Intent(); * intent.setAction("android.intent.action.BROADCAST") * */ //通過帶參構造方法實例化Intent,與上面效果相同 Intent intent = new Intent("android.intent.action.BROADCAST"); //為意向添加數據(鍵值對形式) intent.putExtra("msg","Hello, I am InterfaceContactFragment"); //實例化LocalBroadcastManager類 LocalBroadcastManager broadcastManager =LocalBroadcastManager.getInstance(getActivity()); //發送隱式broadcast broadcastManager.sendBroadcast(intent); Log.d("onActivityCreated","onActivityCreated()"); } }
(2)SecondActivity.java宿主或其他Activity注冊廣播接收者接收廣播
package com.example.fragmentdemo; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.AppCompatActivity; import android.util.Log; /** * Created by elimy on 2016-08-23. */ public class SecondActivity extends AppCompatActivity{ BroadcastReceiver broadcastReceiver; LocalBroadcastManager broadcastManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); //實例化broadcastManager broadcastManager =LocalBroadcastManager.getInstance(this); //實例化過濾器onReceive IntentFilter filter = new IntentFilter("android.intent.action.BROADCAST"); //實例化BroadcastReceiver並實現繼承方法接收數據 broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //根據鍵值獲取消息 Log.d("","onReceive()"); String s =intent.getStringExtra("msg"); Log.d("GetMessage",s); } }; //動態注冊廣播接收者 broadcastManager.registerReceiver(broadcastReceiver,filter); } @Override protected void onDestroy() { super.onDestroy(); Log.d("onDestroy","onDestroy()"); broadcastManager.unregisterReceiver(broadcastReceiver); } }
由於android系統中應用程序之間不能共享內存。因此,在不同應用程序之間交互數據(跨進程通訊)就稍微麻煩一些。在android SDK中提供了4種用於跨進程通訊的方式。
今年春節晚會沒看盡興,被支付寶集福給添了一段插曲,朋友們都在那數定時間段不停的咻一咻,哇,我咻到一個敬業福,不可能的,哈哈。那麼咻一咻功能基於程序代碼是怎麼實現的呢?下面
根據EditText搜索框ListView動態顯示數據是根據需求來的,覺得這之中涉及的東西可能比較的有意思,所以動手來寫一寫,希望對大家有點幫助。首先,我們來分析下整個過
前沿: 很久沒有寫過博客了,因為工作的原因很少有時間寫東西了,最近想寫一個UI系列的博客,因為我發現這一系列的都很少,而且沒有那麼系統,這裡我想以我自己的觀點來闡述一下如