在已有的android demo上增加一個activity


安卓小白,準備在已有的一個demo上添加一個activity,封裝所有待測試的接口,參照《android 編程權威指南》操作。


1、修改包名

修改demo包名,避免與測試中安裝的demo衝突。參考一下鏈接:

http://jingyan.baidu.com/article/380abd0a71a1061d90192cfd.html

修改之後經常提示報錯,無法生成新的R文件,需要仔細檢查資源文件裏未修改的包名。


2、打開strings.xml,添加字符串資源。

    <string name="action_test">測試</string>
</resources>

3、右鍵layout目錄,new->other,在android文件夾中選擇 XML Layout File


4創建新的activity子類

在母包右鍵選擇new->class, 注意繼承android.app.activity

       

 覆蓋onCreate()

	@Override
<span style="white-space:pre">	</span>protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);
	}

5、在manifest中聲明activity,參照以前已有的activity聲明

     <activity
            android:name="com.qq.xgtest.TestActivity"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>


6、在mainavtivity中添加跳轉到testactivity的按鈕

demo有個menu_item.xml

          <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/fuxk_base_divide_line_color" /> 
     <TextView
            android:id="@+id/action_diagnosis"
            android:layout_width="match_parent"
            android:layout_height="@dimen/qq_dimen_80px"
            android:gravity="center"
            android:singleLine="true"
            android:text="@string/action_diagnosis"
            android:textColor="@android:color/white" />

至此,mainactivity上已有“測試”的按鈕,接下來解決點擊按鈕啓動testactivity的問題。


7、demo的showSpinner()方法中有onClickListener()方法 監聽對幾個avtivity跳轉,參考已有代碼,添加:

TextView action_test = (TextView) v  
.findViewById(R.id.action_test);
......
action_test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pw.dismiss();
Intent settingIntent = new Intent();
settingIntent.setClass(context, TestActivity.class);
startActivity(settingIntent);
}
});

至此,點擊“測試“,能跳轉到test activity了,但是頁面是一片黑的,需要修改資源文件,

在activity_test.xml中添加一行 

       

android:background="@android:color/white" >

 即可

此時頁面簡單,無圖標無title,以後需要再添加。


8、接下來在test activity中添加按鈕,封裝測試接口。

在strings.xml中添加

<string name="first_button">基本功能測試</string>
        <string name="second_button">推送功能測試</string>

在activity_test.xml中添加

 

  <Button
        android:id="@+id/first_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:text="@string/first_button" />
    
    <Button
        android:id="@+id/second_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:text="@string/second_button" />



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章