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

来源:互联网 发布:h3c mac地址绑定 编辑:程序博客网 时间:2024/04/29 08:14

弄了这么多天终于能跳转了,哭瞎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);
}
});
}

0 0
原创粉丝点击