• Android深入浅出系列之实例应用—手机页面之间的跳转


      在网页里,我们可以通过超级链接从一个网页跳转到另外一个网页,在手机里面,要如何实现手机页面之间的跳转呢?
      原理:通过布局文件和setContentView()方法配合来实现。通过点击第一个布局文件main.xml当中的按钮,加载第二个布局文件main2.xml,然后点击第二个布局文件main2.xml当中的按钮,加载第一个布局文件main.xml。

      1.1:第一个布局文件main.xml

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
         >
        <TextView  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="@string/hello"
          />
          <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="跳到第二个手机页面"
            android:id="@+id/btn1"
          />
      </LinearLayout>

      1.2:第二个布局文件main2.xml

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
         >
      <TextView  
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:text="@string/hello2"
          />
        <Button
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="跳到第一个手机页面"
          android:id="@+id/btn2"
          />
      </LinearLayout>
      1.3:字符文件stings.xml

      <?xml version="1.0" encoding="utf-8"?>
      <resources>
         <string name="hello">我是第一个手机布局页面</string>
         <string name="hello2">我是第二个手机布局页面</string>
         <string name="app_name">setContentViewDemo</string>
      </resources>
      1.4:代码文件

      package com.menglin.setcontentview;

      import android.app.Activity;
      import android.os.Bundle;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;

      public class MainActivity extends Activity
      {
         private Button btn1 = null;
         private Button btn2 = null;
         @Override
         public void onCreate(Bundle savedInstanceState)
         {
            super.onCreate(savedInstanceState);
            //默认加载第一个布局文件
            setContentView(R.layout.main);
            //通过findViewById()方法得到第一个布局文件中的Button对象
            btn1 = (Button) findViewById(R.id.btn1);
            //给这个Button对象绑定监听器
            btn1.setOnClickListener(new Button1Listener());
         }
     
         //第一个布局文件中的按钮的监听器
         private class Button1Listener implements OnClickListener
           {
            @Override
            public void onClick(View v)
            {   
               //加载第二个布局文件
               setContentView(R.layout.main2);
               //通过findViewById()方法得到第二个布局文件中的Button对象
               btn2 = (Button) findViewById(R.id.btn2);
               //给这个Button对象绑定监听器
               btn2.setOnClickListener(new Button2Listener());
            }           
            }
     
         //第二个布局文件中的按钮的监听器
         private class Button2Listener implements OnClickListener
           {
            @Override
            public void onClick(View v)
            {   
               //加载第一个布局文件
               setContentView(R.layout.main);
               //通过findViewById()方法得到第一个布局文件中的Button对象
               btn1 = (Button) findViewById(R.id.btn1);
               //给这个Button对象绑定监听器
               btn1.setOnClickListener(new Button1Listener());
            }           
            }
      }

      注意:虽然是实现了界面的来回跳转,但是始终是同一个Activity,所以类变量,函数等都是公用的。

      运行效果如下

          当我们单击第一布局文件当中的按钮后,就会切换到第二个我们设计好的布局文件。

      

        

  • 相关阅读:
    C# DataGridView导出到Excel
    面试时,怎样“谈薪”?
    7月11号 列表类型的内置方法及应用
    6月1号 字符编码
    文件相关知识
    Django框架之第二篇app注册、静态文件配置、form表单提交、pycharm链接数据库、Django使用mysql数据库、表字段的增删改查、表数据的增删改查
    函数相关知识
    Django框架之第三篇(路由层)有名/无名分组、反向解析、路由分发、名称空间、伪静态、图书管理系统表设计
    初识Django
    Django框架之第五篇(模型层)单表操作(增删改查)、单表查询和必知必会13条、单表查询之双下划线、Django ORM常用字段和字段参数和关系字段
  • 原文地址:https://www.cnblogs.com/menglin2010/p/2246287.html
Copyright © 2020-2023  润新知