Android(X)多页面

本文详细介绍了Android应用中如何进行页面跳转,包括从主页面到其他页面以及从其他页面返回主页面的通信方式。通过Intent传递数据,并讲解了创建监听器类以实现更复杂的页面交互。此外,还探讨了在多页面应用中如何利用Frame布局进行页面管理。

注意

  1. 添加setContentView(R.layout.activity_main);

页面的跳转

  1. 在安卓中每个页面我们称为一个Activity,Activity之间使用Intent进行通信

  2. 打开页面

    Intent intent = new Intent(NewAvtivity.this,ThirdPage.class);
     startActivity(intent);
    
  3. 关闭页面

      finish(); //关闭当前Activity
    

页面间通信(主页面到其他页面)

  1. 页面间使用Bundle进行通信,所有的数据都传输到Bundle里面

  2. 发送方(按键监听函数)

     button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String input_text = ((EditText)findViewById(R.id.input_sth)).getText().toString();
                    Bundle bundle = new Bundle();
                    bundle.putCharSequence("input",input_text); //将信息放入Bundle
                    Intent intent = new Intent(MainActivity.this,NewAvtivity.class);
                    intent.putExtras(bundle);//将bundle放到intent里面
                    startActivity(intent);//开启另一个页面
                }
            });
    
  3. 接受方(文本框)

            Intent intent = getIntent(); //实例化Intent
            Bundle bundle = intent.getExtras();//实例化Bundle,获取消息
            TextView textView = (TextView)findViewById(R.id.new_page_text);
            textView.setText(bundle.getString("input"));//将消息放入文本框中
    

页面间通信(其他页面到主页面)

  1. 发送方

    but1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = getIntent();
                    Bundle bundle = new Bundle();
                    bundle.putString("username",username.getText().toString());
                    bundle.putString("passwd",passwd.getText().toString());
    
                    intent.putExtras(bundle);
                    setResult(0x11,intent);
                    finish();
                }
            });
    
  2. 接收方

    public class MainActivity extends Activity {
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button openbut = (Button)findViewById(R.id.open_new);
            openbut.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this,NewAvtivity.class);
                    startActivityForResult(intent,0x11);
    
                }
            });
    
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode==0x11&&resultCode==0x11){
                Bundle bundle = data.getExtras();
                String str1 = bundle.getString("username");
                String str2 = bundle.getString("passwd");
                TextView un = (TextView) findViewById(R.id.username);
                TextView ps = (TextView) findViewById(R.id.passwd);
                un.setText(str1);
                ps.setText(str2);
    
    
            }
        }
    }
    

创建监听器类

View.OnClickListener l = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            
        }
    }

多页面和Frame

  1. 两个帧java文件:新建类继承Frame,重写 onCreateView方法,写两个这样的类
package com.example.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class me_fragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view  =inflater.inflate(R.layout.me_fragment,null);
        return view;
    }
}
  1. 主java文件
package com.example.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imageView1 = findViewById(R.id.img1);//获取控件
        ImageView imageView2 = findViewById(R.id.img2);//获取控件

        imageView1.setOnClickListener(l);//设置监听器
        imageView2.setOnClickListener(l);//设置监听器


    }
    View.OnClickListener l = new View.OnClickListener() {//监听器
        @Override
        public void onClick(View v) {
            FragmentManager fm = getSupportFragmentManager();//帧管理器
            FragmentTransaction ft = fm.beginTransaction();
            Fragment f=null;//实例化帧对象指向空

            switch (v.getId()){
                case R.id.img1://如果第一个方块被点击
                    f= new wechat_fragment();//导入新的帧
                    break;
                case R.id.img2://如果第二个方块被点击
                    f= new me_fragment();//导入新的帧
                    break;

            }
            ft.replace(R.id.fragment,f);//替换
            ft.commit();//提交
      }
    };
}
  1. 两个帧xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#008c8c"
            android:scaleType="fitXY"/>
    
    </RelativeLayout>
    
  2. 主xml文件(开始的帧布局采用的是其中一个帧xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
<fragment
    android:layout_width="match_parent"
    android:layout_height="600dp"
    android:name="com.example.activity.wechat_fragment"
    android:id="@+id/fragment"/>

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="50dp"
       android:orientation="horizontal"
       android:layout_marginBottom="0dp">

      <ImageView
         android:id="@+id/img1"
          android:layout_width="10dp"
          android:layout_height="50dp"
         android:layout_weight="1"
         android:background="#FF0000"/>
      <ImageView
          android:id="@+id/img2"
          android:layout_width="10dp"
          android:layout_height="50dp"
          android:layout_weight="1"
          android:background="#00FF00"/>
   </LinearLayout>
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值