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

就是简单的设置下 点击button然后跳转到另一个页面的代码 谢谢

首先在在AndroidManifest.xml里配置
<!-- 添加第二个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"
/>

配置第二个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可以跳转到第二个页面

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);
}
});
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-08-27
你的button不在Activity中的话,就用上下文来启动,也就是你上面的mContext来启动,startActivity(new Intent(mContext,xxx.class))
改为mContext.startActivity(new Intent(xxx));

其实之前的startActivity不行就是因为他默认是this.startActivity,你当前类是Activity子类的时候才行,要不然就手动用上下文环境来启动。

所以多了解代码底层功能好点,一般我setView什么的都是this.setView来,记清楚明白,还能让eclipse自动提示呢。。。
第2个回答  推荐于2016-05-16
btn_save.setOnClickListener(new View.OnClickListener()
{
   @Override
   public void onClick(View view)
   {
    Intent intent = new Intent(当前的Activity.this, 要跳转的Activity.class);
    startActivity(intent);
   }
 });

其中btn_save就是button按钮

第3个回答  推荐于2017-11-24
button触发事件,
然后用Intent就可以了
Intent intent = new Intent(本activity.this,要跳转的activity.class);
startActivity(intent);本回答被提问者采纳
第4个回答  2011-08-27
设置按钮监听,setOnClickListener,然后在监听中写你想做的代码。