android页面跳转
2016-07-09 20:19
369 查看
新建出一个项目
我们说一下layout_width和layout_height
这两个属性是控制控件的大小的
wrap_content 适合内容大小
fill_parent 占满父布局剩余的空间
math_parent 占满全部父布局空间
100dp 固定100dp
运行起来就可以看到效果了
大家可能会发现在第二个页面按返回键会返回到一个页面上,我们有时候的期望并不这样,是想在跳转的时候结束掉第一个页面,这样的话我们可以在
MainActivity.java
一 第一个页面的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.javapackage 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(); } }这样就完成了
相关文章推荐
- android 内存进程管理分析
- andriod 自动更新开发
- 如何讓Android系統顯示CJK擴展區漢字
- Android之自定义搜索框
- android debug sp方法
- android两个时间比对工具类
- TensorFlow 在android上的Demo(1)
- android studio 检查内存泄漏
- 安卓屏幕快速适配大全
- Android 事件总线OTTO入门
- andriod 打造炫酷的电影票在线选座控件,1比1还原淘宝电影在线选座功能
- Android中View绘制流程分析
- 国内安装Android Studio
- Android中ViewPager+Fragment预加载和onResume方法的问题
- 我的Android进阶之旅------>Android Listview跳到指定条目位置的两种实现方法
- 安卓知识点
- 浅显易懂的Activity生命周期
- cannot resolve symbol R解决方法。
- Android设置背景色
- adb连接Android 4.2.2版本手机时出现offline或unauthorized问题的解决方法