安卓小白,准备在已有的一个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" />