博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android脚步---不同界面之间切换
阅读量:7167 次
发布时间:2019-06-29

本文共 8258 字,大约阅读时间需要 27 分钟。

对于一个app,可能需要多个界面,使用Button或者其他控件在不同界面之间切换,那么如何做到呢

首先必须明确,一般一个activity.java文件一般只对应一个界面即一个layout.xml文件,还有可能是在一个界面上再引入一个半屏或者全屏,这样需要用到一些手段,例子在下面放出来。

通常需要新建一个java文件,然后进行处理,新建java文件过程如下:New-class-

 

 输入Name,选择继承的类,一般都是选择最通用的安卓原生的activity类,finish之后得到一个新的java文件,为了方便,可以右键,source-override,选择方法重载,比如,oncreate 等

以自己接触到的代码为例:

 

mTestDialog = new CustomDialog.Builder(this).create(testView2,                R.style.FullScreenDialog, Gravity.NO_GRAVITY);        mTestDialog.show();

引入一个全屏文件,新建一个CustomDialog.Builder,建立一个testvie2的全屏xml文件的关联dialog,然后显示这个dialog,

CustomDialog.java文件

package com.leadcore.uudatoutie.ui; import com.leadcore.uudatoutie.PhotoUI;import com.leadcore.uudatoutie.R;import com.leadcore.uudatoutie.util.LogMan;import android.app.Dialog;import android.content.Context;import android.content.DialogInterface;import android.graphics.drawable.Drawable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.LinearLayout;import android.widget.TextView; /** *  * Create custom Dialog windows for your application * Custom dialogs rely on custom layouts wich allow you to  * create and use your own look & feel. *  * Under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html *  * @author antoine vianey * */public class CustomDialog extends Dialog {    private static final String TAG = "CustomDialog";     private static View layout;        public CustomDialog(Context context, int theme) {        super(context, theme);    }     public CustomDialog(Context context) {        super(context);    }    @Override    public void setTitle(CharSequence title) {        ((TextView) layout.findViewById(R.id.title)).setText(title);    }        public void setIcon(Drawable drawable) {        ((TextView) layout.findViewById(R.id.title)).setCompoundDrawables(drawable, null, null, null);    }     /**     * Helper class for creating a custom dialog     */    public static class Builder {         private Context context;        private String title;        private String message;        private String positiveButtonText;//        private String negativeButtonText;        private View contentView;         private DialogInterface.OnClickListener                         positiveButtonClickListener;//                        negativeButtonClickListener;         public Builder(Context context) {            this.context = context;        }         /**         * Set the Dialog message from String         * @param title         * @return         */        public Builder setMessage(String message) {            this.message = message;            return this;        }         /**         * Set the Dialog message from resource         * @param title         * @return         */        public Builder setMessage(int message) {            this.message = (String) context.getText(message);            return this;        }         /**         * Set the Dialog title from resource         * @param title         * @return         */        public Builder setTitle(int title) {            this.title = (String) context.getText(title);            return this;        }         /**         * Set the Dialog title from String         * @param title         * @return         */        public Builder setTitle(String title) {            this.title = title;            return this;        }         /**         * Set a custom content view for the Dialog.         * If a message is set, the contentView is not         * added to the Dialog...         * @param v         * @return         */        public Builder setContentView(View v) {            this.contentView = v;            return this;        }         /**         * Set the positive button resource and it's listener         * @param positiveButtonText         * @param listener         * @return         */        public Builder setPositiveButton(int positiveButtonText,                DialogInterface.OnClickListener listener) {            this.positiveButtonText = (String) context                    .getText(positiveButtonText);            this.positiveButtonClickListener = listener;            return this;        }         /**         * Set the positive button text and it's listener         * @param positiveButtonText         * @param listener         * @return         */        public Builder setPositiveButton(String positiveButtonText,                DialogInterface.OnClickListener listener) {            this.positiveButtonText = positiveButtonText;            this.positiveButtonClickListener = listener;            return this;        }         /**         * Set the negative button resource and it's listener         * @param negativeButtonText         * @param listener         * @return         *///        public Builder setNegativeButton(int negativeButtonText,//                DialogInterface.OnClickListener listener) {//            this.negativeButtonText = (String) context//                    .getText(negativeButtonText);//            this.negativeButtonClickListener = listener;//            return this;//        }         /**         * Set the negative button text and it's listener         * @param negativeButtonText         * @param listener         * @return         *///        public Builder setNegativeButton(String negativeButtonText,//                DialogInterface.OnClickListener listener) {//            this.negativeButtonText = negativeButtonText;//            this.negativeButtonClickListener = listener;//            return this;//        }         /**         * Create the custom dialog         */        public CustomDialog create(View view, int style, int gravity) {            LayoutInflater inflater = (LayoutInflater) context                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);            // instantiate the dialog with the custom Theme            final CustomDialog dialog = new CustomDialog(context, style);            layout = view;//            diaLogMan.addContentView(layout, new LayoutParams(//                  LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));            // set the dialog title            if(title!=null){                ((TextView) layout.findViewById(R.id.title)).setText(title);            }            // set the confirm button            if (positiveButtonText != null) {                ((Button) layout.findViewById(R.id.cancelButton))                        .setText(positiveButtonText);                if (positiveButtonClickListener != null) {                    ((Button) layout.findViewById(R.id.cancelButton))                            .setOnClickListener(new View.OnClickListener() {                                public void onClick(View v) {                                    LogMan.e(TAG,"positiveButtonClickListener");                                    positiveButtonClickListener.onClick(                                            dialog,                                             DialogInterface.BUTTON_POSITIVE);                                    dialog.dismiss();                                }                            });                }            } else {//                // if no confirm button just set the visibility to GONE//                layout.findViewById(R.id.cancelButton).setVisibility(//                        View.GONE);            }            // set the content message            if (message != null) {                ((TextView) layout.findViewById(                        R.id.message)).setText(message);            } else if (contentView != null) {                // if no message set                // add the contentView to the dialog body                ((LinearLayout) layout.findViewById(R.id.content))                        .removeAllViews();                ((LinearLayout) layout.findViewById(R.id.content))                        .addView(contentView,                                 new LayoutParams(                                        LayoutParams.FILL_PARENT,                                         LayoutParams.WRAP_CONTENT));            }            dialog.getWindow().setGravity(gravity);            dialog.setContentView(layout);            return dialog;        }     } }

如何在其他应用里面仿照这个方法不单要CustomDialog.java文件,还需要扩展Custom-dialog.xml文件

 

转载地址:http://lnmwm.baihongyu.com/

你可能感兴趣的文章
Truncate/Delete/Drop table的特点和区别
查看>>
我的友情链接
查看>>
nginx http core模块学习
查看>>
逢二进一 、逢八进一、逢十六进一
查看>>
搞懂 JAVA 内部类
查看>>
Android中创建与几种解析xml的方法!
查看>>
程序员有趣的十八个事实
查看>>
数据库导出数据字典
查看>>
scala中的option[T]、Any、Nothing、Null和Nil
查看>>
面试算法
查看>>
activemq cluster安装
查看>>
zabbix snmp 常见OID
查看>>
spring cloud 与 docker-compose构建微服务
查看>>
Ext4 Disk Layout
查看>>
rrdtool 详解
查看>>
firefox NS_ERROR_DOM_BAD_URI: Access to restricted URI denied
查看>>
Git常见相关知识与命令
查看>>
lvs(Linux Virtual Server)浅析----------------lvs-nat和lvs-dr实现
查看>>
PostgreSQL pg_rewind report error退出分析
查看>>
漫画 :Apache Nginx80 端口争夺战
查看>>