在已有的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" />



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值