栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用Fragment --- 自定义底部按钮导航

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Fragment --- 自定义底部按钮导航

Main_Active.java

package com.panhua.music;

import androidx.annotation.ColorRes;

import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.Fragment;

import androidx.fragment.app.FragmentManager;

import androidx.fragment.app.FragmentTransaction;

import androidx.viewpager.widget.PagerAdapter;

import androidx.viewpager.widget.ViewPager;

import android.content.Intent;

import android.gesture.Gesture;

import android.gesture.GestureLibraries;

import android.gesture.GestureLibrary;

import android.gesture.GestureOverlayView;

import android.gesture.Prediction;

import android.graphics.Color;

import android.graphics.ColorFilter;

import android.graphics.ColorSpace;

import android.graphics.PorterDuff;

import android.graphics.Region;

import android.graphics.drawable.Drawable;

import android.net.Uri;

import android.os.Bundle;

import android.os.Handler;

import android.text.Layout;

import android.text.method.Touch;

import android.util.Log;

import android.view.Display;

import android.view.GestureDetector;

import android.view.Gravity;

import android.view.KeyEvent;

import android.view.MotionEvent;

import android.view.View;

import android.view.ViewGroup;

import android.view.ViewTreeObserver;

import android.view.WindowManager;

import android.view.animation.Animation;

import android.view.animation.ScaleAnimation;

import android.view.animation.TranslateAnimation;

import android.widget.Button;

import android.widget.EditText;

import android.widget.frameLayout;

import android.widget.ImageButton;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.RelativeLayout;

import android.widget.TextView;

import android.widget.Toast;

import java.lang.reflect.Array;

import java.util.ArrayList;

import java.util.GregorianCalendar;

import java.util.List;

import java.util.Properties;

public class Main_Active extends AppCompatActivity implements View.OnClickListener {

    private frameLayout frameLayout;

    private LinearLayout Drawer_Top;

    private The_global_method the_global_method;

    private int Exit_index = 1;

    public void Layout_show_hide(final boolean bo) {

        int status_height = getResources().getDimensionPixelSize(getResources().getIdentifier("status_bar_height", "dimen", "android"));

        TranslateAnimation translateAnimation;

        if (bo) {

            translateAnimation = new TranslateAnimation(0, 0, 0,

                    getWindowManager().getDefaultDisplay().getHeight() - status_height);

        } else {

            findViewById(R.id.Bottom_Layout).setVisibility(View.VISIBLE);

            translateAnimation = new TranslateAnimation(0, 0, 0,

                    -getWindowManager().getDefaultDisplay().getHeight() - status_height);

        }

        translateAnimation.setDuration(500);

        translateAnimation.setFillAfter(true);

        Drawer_Top.startAnimation(translateAnimation);

        translateAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override

            public void onAnimationStart(Animation animation) {

            }

            @Override

            public void onAnimationEnd(Animation animation) {

                if (bo) {

                    findViewById(R.id.Bottom_Layout).setVisibility(View.GONE);

                    Drawer_Top.clearAnimation();

                    RelativeLayout.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) Drawer_Top.getLayoutParams();

                    marginLayoutParams.setMargins(0, 0, 0, 0);

                    Drawer_Top.setLayoutParams(marginLayoutParams);

                } else {

                    Drawer_Top.clearAnimation();

                    RelativeLayout.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) Drawer_Top.getLayoutParams();

                    marginLayoutParams.setMargins(0, -getWindowManager().getDefaultDisplay().getHeight(), 0, 0);

                    Drawer_Top.setLayoutParams(marginLayoutParams);

                }

            }

            @Override

            public void onAnimationRepeat(Animation animation) {

            }

        });

    }

    public void Drawer_top() {

        RelativeLayout.MarginLayoutParams layoutParams = (RelativeLayout.MarginLayoutParams) Drawer_Top.getLayoutParams();

        layoutParams.width = getWindowManager().getDefaultDisplay().getWidth();

        layoutParams.height = getWindowManager().getDefaultDisplay().getHeight();

        layoutParams.setMargins(0, -getWindowManager().getDefaultDisplay().getHeight(), 0, 0);

        Drawer_Top.setLayoutParams(layoutParams);

    }

    //    按钮Drawer图片设定

    public void Button_Drawable_ico(int view_id, int img_id, boolean boolen) {

        Button view = findViewById(view_id);

        Drawable drawable = getResources().getDrawable(img_id);

        int drawable_top = new The_global_method(getApplicationContext()).px_To_dp(5);

        int drawable_width = new The_global_method(getApplicationContext()).px_To_dp(20);

        int drawable_height = new The_global_method(getApplicationContext()).px_To_dp(25);

        drawable.setBounds(0, drawable_top, drawable_width, drawable_height);

        if (boolen) {

            drawable.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP);

        } else {

            drawable.setColorFilter(Color.parseColor("#FFFFFF"), PorterDuff.Mode.SRC_ATOP);

        }

        view.setCompoundDrawables(null, drawable, null, null);

    }

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_active);

        Drawer_Top = findViewById(R.id.drawer_top);

        Drawer_top();

        findViewById(R.id.button1).setOnClickListener(this);

        findViewById(R.id.button2).setOnClickListener(this);

        findViewById(R.id.button3).setOnClickListener(this);

        findViewById(R.id.button4).setOnClickListener(this);

        findViewById(R.id.button1).callOnClick();

        findViewById(R.id.tools_top).setOnClickListener(this);

        findViewById(R.id.frame_exit).setOnClickListener(this);

        the_global_method = new The_global_method(getApplicationContext());

    }

    @Override

    public void onWindowFocusChanged(boolean hasFocus) {

        super.onWindowFocusChanged(hasFocus);

        Drawable drawable = getResources().getDrawable(R.drawable.main_input_drawable);

        drawable.setBounds(

                findViewById(R.id.input_seek).getWidth()/2 - the_global_method.px_To_dp(25),

                0,

                findViewById(R.id.input_seek).getWidth()/2 - the_global_method.px_To_dp(5),

                the_global_method.px_To_dp(20));

        ((Button) findViewById(R.id.input_seek)).setCompoundDrawables(drawable,null,null,null);

    }

    public void ico_true(int index) {

        Button_Drawable_ico(R.id.button1, R.drawable.main_button1, false);

        Button_Drawable_ico(R.id.button2, R.drawable.main_button2, false);

        Button_Drawable_ico(R.id.button3, R.drawable.main_button3, false);

        Button_Drawable_ico(R.id.button4, R.drawable.main_button4, false);

        ((Button) findViewById(R.id.button1)).setTextColor(Color.parseColor("#FFFFFF"));

        ((Button) findViewById(R.id.button2)).setTextColor(Color.parseColor("#FFFFFF"));

        ((Button) findViewById(R.id.button3)).setTextColor(Color.parseColor("#FFFFFF"));

        ((Button) findViewById(R.id.button4)).setTextColor(Color.parseColor("#FFFFFF"));

        switch (index) {

            case 1:

                Button_Drawable_ico(R.id.button1, R.drawable.main_button1, true);

                ((Button) findViewById(R.id.button1)).setTextColor(Color.BLUE);

                getFragmentManager().beginTransaction().replace(R.id.active_frame, new infomation()).commit();

                break;

            case 2:

                Button_Drawable_ico(R.id.button2, R.drawable.main_button2, true);

                ((Button) findViewById(R.id.button2)).setTextColor(Color.BLUE);

                getFragmentManager().beginTransaction().replace(R.id.active_frame, new contact()).commit();

                break;

            case 3:

                Button_Drawable_ico(R.id.button3, R.drawable.main_button3, true);

                ((Button) findViewById(R.id.button3)).setTextColor(Color.BLUE);

                getFragmentManager().beginTransaction().replace(R.id.active_frame, new hotbroadcast()).commit();

                break;

            case 4:

                Button_Drawable_ico(R.id.button4, R.drawable.main_button4, true);

                ((Button) findViewById(R.id.button4)).setTextColor(Color.BLUE);

                getFragmentManager().beginTransaction().replace(R.id.active_frame, new live()).commit();

                break;

        }

    }

    @Override

    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.frame_exit:

                Layout_show_hide(false);

                break;

            case R.id.tools_top:

                Layout_show_hide(true);

                break;

            case R.id.button1:

                ico_true(1);

                break;

            case R.id.button2:

                ico_true(2);

                break;

            case R.id.button3:

                ico_true(3);

                break;

            case R.id.button4:

                ico_true(4);

                break;

        }

    }

    //    两次返回

    @Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {

            if (Exit_index == 2) {

                finish();

                return super.onKeyDown(keyCode, event);

            } else {

                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();

                Exit_index += Exit_index;

                new Thread(new Runnable() {

                    @Override

                    public void run() {

                        try {

                            Thread.sleep(2000);

                        } catch (Exception e) {

                        }

                        Exit_index = 1;

                    }

                }).start();

            }

        }

        return false;

    }

}


The_global_method.java

package com.panhua.music;

import android.content.Context;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

import android.os.Handler;

import android.os.Looper;

import android.os.NetworkOnMainThreadException;

import android.text.Layout;

import android.util.Log;

import android.view.Gravity;

import android.view.KeyEvent;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.EditText;

import android.widget.GridLayout;

import android.widget.TextView;

import android.widget.Toast;

import androidx.annotation.Dimension;

import androidx.annotation.Nullable;

import androidx.appcompat.app.AppCompatActivity;

import androidx.viewpager.widget.ViewPager;

import org.json.JSONObject;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.lang.reflect.Array;

import java.net.HttpURLConnection;

import java.net.URL;



public class The_global_method extends AppCompatActivity{

    Context context;

    View view_layout;

    int Exit_index;

    String TAG = "程序日志";

    String server_string;

    JSONObject server_json;

    int server_code;

    //    主函数

    public The_global_method(Context context) {

        this.context = context;

        this.Exit_index = 1;

    }

    //  屏幕dp转px

    public int px_To_dp(int dp_number) {

        float px = context.getResources().getDisplayMetrics().density;

        return (int) (dp_number * px + 0.5f);

    }

    public HttpURLConnection Url(int Port, String Json) {

        HttpURLConnection httpURLConnection = null;

        try {

            URL url = new URL("http://103.45.144.254:" + Port + "/?" + Json);

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setConnectTimeout(3000);

            connection.setReadTimeout(3000);

            connection.setRequestMethod("GET");

            connection.setDoInput(true);

            connection.setDoOutput(false);

            connection.setUseCaches(false);

            httpURLConnection = connection;

        } catch (Exception e) {

            Log.d(TAG, "Url: " + e);

        }

        return httpURLConnection;

    }

    public void Server(final int Port, final String Json) {

        final HttpURLConnection httpURLConnection = Url(Port, Json);

        new Thread(new Runnable() {

            @Override

            public void run() {

                try {

                    server_code = httpURLConnection.getResponseCode();

                    if (httpURLConnection.getResponseCode() == 200) {

                        InputStream inputStream = httpURLConnection.getInputStream();

                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                        int len = 0;

                        byte[] bytes = new byte[1024];

                        while ((len = inputStream.read(bytes)) != -1) {

                            byteArrayOutputStream.write(bytes, 0, len);

                        }

                        inputStream.close();

                        byteArrayOutputStream.close();

                        String string = new String(byteArrayOutputStream.toByteArray());

                        JSONObject jsonObject = new JSONObject(string.toString());

                        server_string = string;

                        server_json = jsonObject;

                    }

                } catch (Exception e) {

                    Log.d(TAG, "Server: " + e);

                }

            }

        }).start();

    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/490275.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号