Android Studio学习路程(6)

今天学习了如何进行界面之间的跳转。

用Intent进行页面跳转:

    Intent是一个将要执行的动作的抽象的描述,由Intent来协助完成Android各个组件之间的通讯。下面是一些代码。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     android:orientation="vertical"
11     tools:context="com.example.hp.app2.MainActivity">
12 
13     <Button
14         android:id="@+id/btn1"
15         android:text="按钮1"
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content" />
18 
19     <Button
20         android:id="@+id/btn2"
21         android:text="按钮2"
22         android:layout_width="match_parent"
23         android:layout_height="wrap_content" />
24 
25 
26 </LinearLayout>
package com.example.hp.app2;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

/**
 * Created by hp on 2020/2/11.
 */
public class FirstActivity extends ActionBarActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

    }


}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:textSize="25dp"
        android:textColor="@android:color/holo_red_light"
        android:text="这是第一个界面"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
 1 package com.example.hp.app2;
 2 
 3 import android.content.Intent;
 4 import android.os.Bundle;
 5 import android.support.v7.app.ActionBarActivity;
 6 import android.view.View;
 7 
 8 public class MainActivity extends ActionBarActivity implements View.OnClickListener {
 9 
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_main);
14         initUI();
15     }
16 
17     private void initUI() {
18         findViewById(R.id.btn1).setOnClickListener( this);
19         findViewById(R.id.btn2).setOnClickListener( this);
20     }
21 
22 
23     @Override
24     public void onClick(View view) {
25         switch (view.getId()){
26             case R.id.btn1:
27                 //跳转到第一个界面
28                 //跳转界面用到了intent的方法
29                 Intent intent = new Intent();
30                 intent.setClass(getApplicationContext(),FirstActivity.class);
31                 break;
32             case R.id.btn2:
33                 //第二个界面
34                 break;
35 
36         }
37     }
38 }

猜你喜欢

转载自www.cnblogs.com/mxk123456/p/12297340.html