zoukankan      html  css  js  c++  java
  • Fragment

    界面的设置和布局

    *利用传统布局来规划界面

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="bottom|center_horizontal"
        android:text="@string/bt_1"
        android:textColor="#DBD"
        android:onClick="onClick"
        android:textSize="20dp"
        android:id="@+id/show">
    
    </TextView>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Show Next Page"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:id="@+id/yes"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </TextView>
    </LinearLayout>
    

    第二个界面的转换

    <FrameLayout 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"
        tools:context="com.example.a15083.fragmentandactivity.FirstFragment">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_blue_light"
            android:gravity="center"
            android:textSize="25sp"
            android:text="@string/first_fragment" />
    
    </FrameLayout>
    

    java代码的实现

    package com.example.bwqpzy.fragment1;
    
    import android.app.Fragment;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Toast;
    
    import com.example.bwqpzy.fragment1.FirstFragment;
    import com.example.bwqpzy.fragment1.SecondFragment;
    
    public class MainActivity extends AppCompatActivity {
        FirstFragment firstFragment;
        SecondFragment secondFragment;
        private boolean qh = true;
        private boolean tc= false;
    
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            FragmentManager manager = getFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
    
            firstFragment = new FirstFragment();
            transaction.add(R.id.yes,firstFragment);
            transaction.commit();
    
        }
    
    
    
        public void onClick(View view) {
            if(view.getId() == R.id.yes){
                tc = true;
                if(qh){
                    FragmentManager manager = getFragmentManager();
                    FragmentTransaction transaction = manager.beginTransaction();
                    if (secondFragment == null){
                        secondFragment = new SecondFragment();
                        transaction.replace(R.id.next,secondFragment);
                        transaction.commit();
                        qh = false;
                    } else{
                        transaction.replace(R.id.yes,secondFragment);
                        transaction.commit();
                        qh = false;
                    }
                }else{
                    Toast.makeText(this,"This is second fragment",Toast.LENGTH_SHORT).show();
                }
    
            }
        }
        public boolean onKeyDown(int keyCode, KeyEvent event) {
    
            if(event.getKeyCode()== KeyEvent.KEYCODE_BACK&&tc){
    
                FragmentManager manager = getFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                qh = true;
                tc = false;
                transaction.replace(R.id.yes,firstFragment);
                transaction.commit();
                return  false;
            } else {
                finish();
            }
            return super.onKeyDown(keyCode, event);
    
    
        }
    }
    
  • 相关阅读:
    【Django】DRF序列化器之Serializers家族
    【Django】DRF源码分析之五大模块
    【Django】DRF源码分析对比原生Django
    【MySQL】主从复制
    【MySQL】锁之InnoDB
    【MySQL】锁之MyISAM
    【安装】Ubuntu之Redis
    Git安装配置以及常用命令整理
    10、Python 数据分析-Matplotlib绘图大全详解
    8、Python 数据分析-Pandas高级操作
  • 原文地址:https://www.cnblogs.com/YUESEtaimei/p/6730863.html
Copyright © 2011-2022 走看看