android中如何设置点击button页面跳转

弄了这么多天终于能跳转了,哭瞎T T

首先在在AndroidManifest.xml里配置(这个得先在src里新建一个OtherActivity.java)
<!-- 添加第二个activity -->
<activity android:name=".OtherActivity" android:label="other Activity"></activity>   

在第一个main layout里面配置一个跳转按钮
<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="打开otheractivity"
android:id="@+id/button"   //id名字记住
/>

配置第二个layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<TextView 
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="other activiy"
/>
</LinearLayout>

待跳转页面的activity,与第二个layout结合.代码如下
public class OtherActivity extends Activity { 
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.other);

}

对主activity配置.点击button可以跳转到第二个页面
(会有很多问题,如果使用Eclipse的话,可以使用快捷键【Ctrl+Shift+o】 ,自动添加相应的import。)

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;


Button button = (Button)this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// 打开另一个activity,通过意图,意图作用是激活其他组件
Intent intent = new Intent();
intent.setClass(MainActivityActivity.this, OtherActivity.class);

//发送意图.将意图发送给android系统,系统根据意图来激活组件
startActivity(intent);
}
});
}

  • 7
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值