安卓页面点击跳转intent

来源:互联网 发布:ubuntu win10启动项 编辑:程序博客网 时间:2024/05/01 16:21

转载请标明出处:http://blog.csdn.net/wu_wxc/article/details/48195163
本文出自【吴孝城的CSDN博客】

安卓各个activity之间跳换用intent意图来实现,

需要编辑的文件如下


先写两个布局文件

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    android:background="#ffffff"    android:orientation="vertical">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="页面一"        android:textSize="20sp"/>    <Button        android:id="@+id/bt1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="跳到页面二"        android:textSize="20sp"/></LinearLayout>

page2.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    android:background="#aaaaaa">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="页面二"        android:textSize="20sp"/></LinearLayout>

然后在Java程序里使用意图跳转页面就行

MainActivity.java

package cn.wuxiaocheng.intent;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {    private Button bt1;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        //通过findviewbyID获得按钮        bt1 = (Button) findViewById(R.id.bt1);        //设置按钮点击事件        bt1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(MainActivity.this, Page2.class);                //启动意图                startActivity(intent);            }        });    }}
第二个activity

Page2.java

package cn.wuxiaocheng.intent;import android.app.Activity;import android.os.Bundle;/** * Created by Administrator on 2015/9/3. */public class Page2 extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.page2);    }}
最后别忘了,activity要在AndroidManifest.xml中注册

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="cn.wuxiaocheng.intent" >    <application        android:allowBackup="true"        android:icon="@mipmap/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=".Page2">        </activity>    </application></manifest>

第十九二十行即为注册activity

运行效果如下




1 0
原创粉丝点击