您的位置:首页 > 移动开发 > Android开发

android页面跳转

2016-07-09 20:19 369 查看
新建出一个项目

一 第一个页面的UI

activity_main.xml

<?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:orientation="horizontal"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="跳转到第二个页面"/>

</LinearLayout>

我们说一下layout_width和layout_height

这两个属性是控制控件的大小的

wrap_content  适合内容大小

fill_parent 占满父布局剩余的空间

math_parent 占满全部父布局空间

100dp  固定100dp

二 书写第一个页面对应的activity代码

MainActivity.java

package com.kzp.csnd.jumpactivity;

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 implements View.OnClickListener {

Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}

private void initView() {
button= (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
break;
}
}
}


三 第二个页面的UI

main2activity.xml

<?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:orientation="horizontal"
tools:context="com.kzp.csnd.jumpactivity.Main2Activity">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="这是第二个页面"/>

</LinearLayout>

运行起来就可以看到效果了
大家可能会发现在第二个页面按返回键会返回到一个页面上,我们有时候的期望并不这样,是想在跳转的时候结束掉第一个页面,这样的话我们可以在

startActivity(intent);后面加上一句finish();那么就会结束掉第一个页面

四 带参数跳转页面

我们在原来的基础上进行书写
MainActivity.java

package com.kzp.csnd.jumpactivity;

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 implements View.OnClickListener {

Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}

private void initView() {
button= (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
Bundle bundle=new Bundle();
bundle.putString("text","这是一个参数");
intent.putExtras(bundle);
startActivity(intent);
break;
}
}
}


Main2Activity.java

package com.kzp.csnd.jumpactivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

Intent intent=getIntent();
Bundle bundle=intent.getExtras();
String text=bundle.getString("text");
Toast.makeText(Main2Activity.this,text,Toast.LENGTH_LONG).show();
}
}
这样就完成了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: