編輯:Android開發教程
Fragment(碎片) 可以靈活地從一個活動的Activity上添加或刪除Fragment, 有良好的用戶體驗;
下面是Fragment的具體設計:
1. 創建new_item_fragment的資源文件:
位置: res->new_item_fragment.xml
<?xml version="1.0" encoding="utf-8"?> <EditText xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/addItemHint" android:contentDescription="@string/addItemContentDescription" />
包含一個EditText控件;
2. 創建NewItemFragment的邏輯實現:
位置: java->package->NewItemFragment.java
package mzx.spike.todolist.app; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; /** * Created by C.L.Wang on 14-3-14. */ public class NewItemFragment extends Fragment{ private OnNewItemAddedListener onNewItemAddedListener; //監聽事件 public interface OnNewItemAddedListener { public void onNewItemAdded(String newItem); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.new_item_fragment, container, false); final EditText myEditText = (EditText)view.findViewById(R.id.myEditText); //監聽事件 myEditText.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View view, int i, KeyEvent keyEvent) { if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) if ((i == KeyEvent.KEYCODE_DPAD_CENTER) || (i == KeyEvent.KEYCODE_ENTER)) { String newItem = myEditText.getText().toString(); onNewItemAddedListener.onNewItemAdded(newItem); myEditText.setText(""); return true; } return false; } }); return view; } //綁定到Activity public void onAttach(Activity activity) { super.onAttach(activity); try { onNewItemAddedListener = (OnNewItemAddedListener)activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnNewItemAddedListener"); } } }
詳解:
1. 繼承Fragment類;
2. 監聽接口(interface OnNewItemListener), 包含一個監聽方法(onNewItemAdded), 然後實現一個實例;
3. 創建視圖(onCreateView), 需要返回一個視圖(View);
4. 填充一個視圖, 調用inflater.inflate()函數, 綁定fragment的資源文件;
5. 獲得控件的引用, view.findViewById(), 得到myEditText的引用;
6. 監聽事件, 監聽myEditText的輸入內容, 傳遞給接口的內容(new Item);
7. 使用onAttach()函數, 綁定Acitivity之後, 獲得activity的引用;
3. 創建ToDoListFragment的邏輯實現:
位置: java->package->ToDoListFragment.java
package mzx.spike.todolist.app; import android.app.ListFragment; /** * Created by C.L.Wang on 14-3-14. */ public class ToDoListFragment extends ListFragment{ }
繼承ListFragment;
4. 創建activity_to_do_list資源文件, 即Activity資源文件:
位置: res->layout->activity_to_do_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="mzx.spike.todolist.app.ToDoListActivity"> <fragment android:name="mzx.spike.todolist.app.NewItemFragment" android:id="@+id/NewItemFragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/addItemHint" android:contentDescription="@string/addItemContentDescription" /> <fragment android:name="mzx.spike.todolist.app.ToDoListFragment" android:id="@+id/ToDoListFragment" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
LinearLayout包含兩個fragment結點, 需要提供android:name的參數, 實現;
 
通過Java包名直接定位到你的Apphttp://market.android.com/details?id=<java包名>或者market://detai
Android中context可以作很多操作,但是最主要的功能是加載和訪問資源。在android中有兩種context,一種是application context,一種
前言TextView的drawableLeft、drawableRight和drawableTop是一個常用、好用的屬性,可以在文本的上下左右放置一個圖片,而不使用更加復
如果不使用系統自帶的TitleBar(即Activity被設置@android:style/Theme.NoTitleBar),那就需要自己來寫進度條了,這裡封裝了一個自
Local Service Controller 是將LocalServ