对于一个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文件

<?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>

android脚步---不同界面之间切换的更多相关文章

  1. android脚步---UI界面修改,增加按钮和监听

    我的UU界面,其布局如下: 需要修改的部分: 意见反馈居中,还有增加backbutton 首先在mainactivity中找到我的UU的定义:dialogue public void showAbou ...

  2. android脚步---不同activity之间参数传递

    现在有两个activity,一个是mainactivity,一个是detectactivity 后者需要调用前者的一个参数,这里用到了intent  getextras(); putextras(); ...

  3. android脚步---UI界面修改,关于activity中增加按钮和监听

    增加按钮和监听,这个和上个不同在于,它不是在一个dialog里面,而是从新写了一个activity,因此需要先找到这个activity的入口. case R.id.checkframe: if (mC ...

  4. 虚拟机+ubuntu 图形界面和终端界面的切换

    虚拟机环境,在图形界面和文本界面间切换:1  VMWare虚拟机下,由图形界面切换到文本界面,和虚拟机设置有关,默认VM占用Ctrl+Alt为热键,所以由图形界面切换到文本界面的组合键为: Ctrl+ ...

  5. Android应用:横竖屏切换总结

    眨眼间,已经到了2016你年春节前,离上一篇博客的时间已经有6个月多,回想起这半年的种种,不得不说,学习和工作实在是太忙了,或许这就是程序员的真实写照吧. 写博客之初,主要的目的还是为了把自己的学习痕 ...

  6. 二、Android应用的界面编程(七)ViewAnimator及其子类[ ViewSwitcher、ImageSwitcher、TextSwitcher、ViewFlipper ]

    ViewAnimator是一个基类,它继承了FrameLayout.因此它表现出FrameLayout的特征,可以将多个View组“叠”在一起. ViewAnimator可以在View切换时表现出动画 ...

  7. Android app应用多语言切换功能实现

    最近在做一个多语言切换的功能,类似于微信的语言切换,搜了下资料基本上都是以下这种: 1. 实现的效果 和微信类似,在设置界面打开切换语言的界面,选择语言后重启 HomeActivity,语言切换完成, ...

  8. Android禁止横屏竖屏切换

    在Android中要让一个程序的界面始终保持一个方向,不随手机方向转动而变化的办法: 只要在AndroidManifest.xml里面配置一下就可以了. 在AndroidManifest.xml的ac ...

  9. Android实现入门界面布局

    Android实现入门界面布局 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 代码实现 首先是常量的定义,安卓中固定字符串应该定义在常量中. stri ...

随机推荐

  1. android开发进阶学习博客资源

    Android开发者博客推荐 Android入门级 - 罗宪明 http://blog.csdn.net/wdaming1986 Android入门级 - 魏祝林 http://blog.csdn.n ...

  2. CodeForces 754C Vladik and chat (DP+暴力)

    题意:给定n个人的m个对话,问能不能找一个方式使得满足,上下楼层人名不同,并且自己不提及自己. 析:首先预处理每一层能有多少个user可选,dp[i][j] 表示第 i 层是不是可以选第 j 个use ...

  3. 错误记录-spring+mybatis

    1.Syntax error on token "String", @ expected 解决:去掉类名后的括号 ps:这错误太二了 2.The nested type UserS ...

  4. hdu_4828_Grids(卡特兰数+逆元)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4828 题意:中文,不解释 题解:实际就是一个卡特兰递推: Catalan(n+1)= Catalan( ...

  5. windows7安装oracle 10g

    1.出现如下错误 解决办法: ①确保你有该文件夹的完全控制权.文件夹点右键->属性->安全->高级->所有者->改为自己->编辑自己的权限为完全控制. ②将setu ...

  6. tar.gz tar.bz2 解压

    从网络上下载到的源码包, 最常见的是 .tar.gz 包, 还有一部分是 .tar.bz2包   要解压很简单 :   .tar.gz     格式解压为          tar   -zxvf   ...

  7. 【jsp 分页】mysql limit方式进行分页

    项目结构示意图: splitPage |-com.balfish.bean     Goods.java |-com.balfish.dao       GoodsDao.java |-com.bal ...

  8. Simple But Useful Samples About 'grep' Command(简单实用的grep 命令)

    Do the following: grep -rnw '/path/to/somewhere/' -e "pattern" -r or -R is recursive, -n i ...

  9. Hibernate 系列教程3-单表操作

    工程截图 hibernate.cfg.xml <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Conf ...

  10. XAMPP(v1.83)中的PHP(v5.5.15)访问SQLServer2014

    驱动安装: 1. 下载SQLServer的微软官方PHP驱动,http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx 2. 安装SQLSRV31 ...