android页面跳转
来源:互联网 发布:linux数据库损坏备份 编辑:程序博客网 时间:2024/06/05 10:53
新建出一个项目
一 第一个页面的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(); }}这样就完成了
2 0
- android 关于页面跳转
- Android页面跳转
- android基础页面跳转
- android基础页面跳转
- android 页面停几秒后跳转
- android页面跳转
- Android页面跳转
- android 页面定时跳转
- android 页面停几秒后跳转
- Android:页面跳转
- android页面跳转
- Android页面跳转
- android 页面跳转
- Android--页面跳转
- android 页面跳转 action
- android 页面跳转总结
- android 的页面跳转
- Android按钮跳转页面
- 7.9学习问题整理
- javascript浏览器对象
- iOS-高德地图
- 线性表之顺序存储结构--C实现
- WAS的异常"webcontainer E com.ibm.ws.webcontainer.WebContainer sendUnavailableException"莫名其妙
- android页面跳转
- linux的GPIO驱动
- NYOJ 55 懒省事的小明 优先队列
- Maven配置
- Oracle数据库的常用操作
- Ubuntu16.04安装phpmyadmin
- 非网址的情况下怎么从a标签跳转到某一个地方
- Javascript 面向对象编程(一):封装
- 数据结构-->栈的顺序实现 ADT