工程的功能是实现在一个acticity上点击按钮,切换到另外一个activity

以下代码为MainActivity.java中的代码

package com.example.button_activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { private Button myButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new MyButtonListener());
} class MyButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, OtherActivity.class);
MainActivity.this.startActivity(intent);
} } }

以下代码为OtherActivity.java中的代码

package com.example.button_activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView; public class OtherActivity extends Activity{ private TextView myTextView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setText(R.string.other);
} }

以下代码为activity_main.xml中的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> </RelativeLayout>

以下代码为activity_other.xml中的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" > <TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> </RelativeLayout>

以下代码为string.xml中的代码

<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">button_activity</string>
<string name="hello_world">Hello world!</string>
<string name="other">OtherActivity</string>
</resources>

以下代码为AndroidManifest.xml中的代码

注意修改package的名称

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.button_activity"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".OtherActivity"
android:label="@string/other" >
</activity>
</application> </manifest>

如果不能运行请kill-adb和start-adb并重新启动eclipse

最新文章

  1. 微信支付开发demo
  2. jQuery的deferred对象详解(二)
  3. angularjs kindEditor 中自定义按钮 弹出dialog
  4. [HDU 2049] 不容易系列之(4)——考新郎 (错排问题)
  5. win7 无线网络无法启动
  6. 【转】android移植之request_suspend_state: wakeup &amp; init: untracked pid xx exited问题的解决
  7. Jquery的bootstrap在线文本编辑器插件Summernote
  8. linux stat系统调用,获取文件信息。
  9. C语言标准库函数qsort具体解释
  10. java.net.NoRouteToHostException: No route to host
  11. 【转载】从头编写 asp.net core 2.0 web api 基础框架 (4) EF配置
  12. 我的POI代码库(持续更新)
  13. [转]python中的正则表达式(re模块)
  14. OD之修改文件标题(一)
  15. linux发送邮件的功能总结
  16. qperf测量网络带宽和延迟
  17. 【PAT】1015 德才论 (25)(25 分)
  18. Django项目启动之前执行流程剖析
  19. pip install read time-out
  20. maven打的包中含源文件jar包

热门文章

  1. HIbernate的增删改
  2. 【BZOJ 2733】【HNOI 2012】永无乡 Splay启发式合并
  3. Eclipse 中 安装 SVN 插件
  4. Fresco内存机制(Ashmem匿名共享内存)
  5. 控制浏览器高度 宽度 只能支持ie
  6. 【BZOJ-2893】征服王 最大费用最大流(带下界最小流)
  7. DataTable是否存在某个列的判断
  8. Android成长日记-Noification实现状态栏通知
  9. android.content.SharedPreferences.edit()
  10. CentOS加载U盘