android脚步---不同界面之间切换
对于一个app,可能需要多个界面,使用Button或者其他控件在不同界面之间切换,那么如何做到呢
首先必须明确,一般一个activity.java文件一般只对应一个界面即一个layout.xml文件,还有可能是在一个界面上再引入一个半屏或者全屏,这样需要用到一些手段,例子在下面放出来。
通常需要新建一个java文件,然后进行处理,新建java文件过程如下:New-class-
输入Name,选择继承的类,一般都是选择最通用的安卓原生的activity类,finish之后得到一个新的java文件,为了方便,可以选择
以自己接触到的代码为例:
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文件
<?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="wrap_content"> <LinearLayout android:orientation="vertical" android:background="@drawable/photoframe_title" android:layout_width="fill_parent" android:layout_height="@dimen/dialog_title_height" android:gravity="center_vertical"> <TextView android:id="@+id/title" android:paddingRight="10dip" android:paddingLeft="10dip" android:layout_width="wrap_content" android:layout_gravity="center" android:layout_height="wrap_content" style="@style/MyDialogTitle"/> </LinearLayout> <LinearLayout android:id="@+id/content" android:orientation="horizontal" android:background="@android:color/white" android:paddingTop="10dp" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/share_weixin" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:padding="5dip" android:drawableTop="@drawable/share_weixin" android:text="@string/review_share_weixin" android:gravity="center_horizontal" android:textColor="@color/text_dialog_button_title" android:background="@drawable/bg_pressed"/> <TextView android:id="@+id/share_weixin_guys" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:padding="5dip" android:drawableTop="@drawable/share_weixin_guys" android:text="@string/review_share_weixin_guys" android:gravity="center_horizontal" android:textColor="@color/text_dialog_button_title" android:background="@drawable/bg_pressed"/> <TextView android:id="@+id/share_sina" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:padding="5dip" android:drawableTop="@drawable/share_sina" android:text="@string/review_share_sina" android:gravity="center_horizontal" android:textColor="@color/text_dialog_button_title" android:background="@drawable/bg_pressed"/> <TextView android:id="@+id/share_more" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:padding="5dip" android:drawableTop="@drawable/share_more" android:text="@string/review_share_more" android:gravity="center_horizontal" android:textColor="@color/text_dialog_button_title" android:background="@drawable/bg_pressed"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:background="@android:color/white" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/cancelButton" android:layout_width="match_parent" style="@style/MyDialogButton" /> </LinearLayout> </LinearLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。