Android Studio学习路程(6)
今天学习了如何进行界面之间的跳转。
用Intent进行页面跳转:
Intent是一个将要执行的动作的抽象的描述,由Intent来协助完成Android各个组件之间的通讯。下面是一些代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.hp.app2.MainActivity">
<Button
android:id="@+id/btn1"
android:text="按钮1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn2"
android:text="按钮2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</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>package com.example.hp.app2;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
}
private void initUI() {
findViewById(R.id.btn1).setOnClickListener( this);
findViewById(R.id.btn2).setOnClickListener( this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn1:
//跳转到第一个界面
//跳转界面用到了intent的方法
Intent intent = new Intent();
intent.setClass(getApplicationContext(),FirstActivity.class);
break;
case R.id.btn2:
//第二个界面
break;
}
}
} 相关推荐
xfcyhades 2020-11-20
Michael 2020-11-03
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28