Android里一般的画面(Activity)都是尽量占满整个屏幕,这样符合单线程的设计,

而有些类似popup之类的子画面,却希望他弹出来的时候表现的如同web的模态窗口

(ModelWindow,Dialog等等),在项目中出现了一个可以把子画面做成类似模态窗口

的例子。

代码

MainActivity.java

 package com.example.testpopactivity;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btnYes = (Button) findViewById(R.id.btnPopup);
btnYes.setOnClickListener(this); } @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnPopup:
Intent intent = new Intent(MainActivity.this, PopupActivity.class);
startActivityForResult(intent,R.layout.activity_popup);
break;
default:
break;
} } @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case R.layout.activity_popup:
String input = (data == null? "" : data.getStringExtra("input"));
if(!"".equals(input)) {
TextView tvLabel = (TextView)findViewById(R.id.tvLabel);
tvLabel.setText(input);
}
break;
default:
break;
}
} }

PopupActivity.java

 package com.example.testpopactivity;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText; public class PopupActivity extends Activity implements OnClickListener { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup); setFinishOnTouchOutside(false); // to set this could prevent window from closing by touching transparent part.
findViewById(R.id.btnOk).setOnClickListener(this); } @Override
public void onClick(View view) {
Intent intent = new Intent();
switch (view.getId()) {
case R.id.btnOk:
String input = ((EditText) findViewById(R.id.etInput)).getText().toString();
intent.putExtra("input", input);
setResult(RESULT_OK, intent);
finish();
break;
default:
break;
}
} }

activity_main.xml

 <RelativeLayout 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"
android:gravity="center"
tools:context=".MainActivity" > <TextView
android:id="@+id/tvLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/btnPopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvLabel"
android:text="@string/btn_popup" /> </RelativeLayout>

activity_popup.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="200dp"
android:layout_height="100dp"
android:gravity="center"
tools:context=".PopupActivity" > <EditText
android:id="@+id/etInput"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:inputType="text"
android:text="@string/str_blank" /> <Button
android:id="@+id/btnOk"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@id/etInput"
android:text="@string/btn_ok" /> </RelativeLayout>

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testpopactivity"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testpopactivity.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.testpopactivity.PopupActivity"
android:label="@string/title_activity_popup"
android:theme="@android:style/Theme.Dialog"
>
</activity>
</application> </manifest>

效果如图:

Android 中类似ModelWindow的一个实现的更多相关文章

  1. Android中关于XML的一个小问题——使用XML输出“<”错误的问题

    在 XML 中,有 5 个预定义的实体引用: < < 小于 > > 大于 & & 和号 &apos; ' 单引号 " " 引号 注释 ...

  2. Android中使用shape制作一个旋转的progressbar

    public class ZNtestResActivity extends Activity { @Override public void onCreate(Bundle savedInstanc ...

  3. [转]Android中Xposed框架篇—利用Xposed框架实现拦截系统方法

    一.前言 关于Xposed框架相信大家应该不陌生了,他是Android中Hook技术的一个著名的框架,还有一个框架是CydiaSubstrate,但是这个框架是收费的,而且个人觉得不怎么好用,而Xpo ...

  4. [转载] Android中Xposed框架篇---利用Xposed框架实现拦截系统方法

    本文转载自: http://www.wjdiankong.cn/android%E4%B8%ADxposed%E6%A1%86%E6%9E%B6%E7%AF%87-%E5%88%A9%E7%94%A8 ...

  5. android中的一些问题

    1. Android dvm的进程和Linux的进程, 应用程序的进程是否为同一个概念 DVM指dalivk的虚拟机.每一个Android应用程序都在它自己的进程中运行,都拥有一个独立的Dalvik虚 ...

  6. Android中的Surface和SurfaceView

    一.什么是Surface 简单的说Surface对应了一块屏幕缓冲区,每个window对应一个Surface,任何View都要画在Surface的Canvas上(后面有原因解释).传统的view共享一 ...

  7. android中SELINUX规则分析和语法简介【转】

    本文转载自:https://blog.csdn.net/LoongEmbedded/article/details/62430039 1. SELINUX是可以理解为一种Android上面的安全机制, ...

  8. android中SELINUX规则分析和语法简介

    1. SELINUX是可以理解为一种android上面的安全机制,是有美国国家安全局和一些公司设计的一个针对linux的安全加强系统我们可以通过配置SELINUX的相关policy,来定制自己的手机的 ...

  9. CocosCreator反射在Android中的使用

    CocosCreator反射在Android中的使用 新建一个CocosCreator项目,然后点击构建 构建完成之后,即可用AndroidStudio打开构建的项目 使用AndroidStudio打 ...

随机推荐

  1. Python中的random模块

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  2. [开发笔记]-DataGridView控件中自定义控件的使用

    最近工作之余在做一个百度歌曲搜索播放的小程序,需要显示歌曲列表的功能.在winform中采用DataGirdView来实现. 很久不写winform程序了,有些控件的用法也有些显得生疏了,特记录一下. ...

  3. sql面向过程用法

    sql可以看成是面向过程的编程语言.该语言中,有string.date.table这样的类型等等 一.操作表 sql相当于一个函数,输入是两个或多个表(A, B, ...) 求集合: 并集 union ...

  4. [转]Putty中文乱码解决方法

    from: http://www.putty.ws/putty-luanma putty使用的过程中,你是否遇到过putty出现中文乱码的情况呢?putty终端出现乱码,是情况,多数是由于系统或软件缺 ...

  5. 登陆中session的处理

    在学校中的登陆注册使用的普通session存储信息,然后就是根据session中获取user是否拥有来判断是否登陆. 在一次面试中别人问到了我你们项目的登陆session是怎么一个情况,我这样答的话那 ...

  6. Unity开发Android应用程序:调用安卓应用程序功能

    开发环境: Eclipse3.4 + adt12 + jdk6 + AndroidSDK2.2 Unity3.4 + windows7 测试设备: HTC Desire HD 本文要涉及到的几个重点问 ...

  7. poj1160 dp

    //Accepted 564 KB 63 ms //和hdu1227一样 //dp[i][j]=min(dp[i][j],dp[k][j-1]+cost[k+1][i]) //初始化条件,dp[0][ ...

  8. form表单select联动

    下拉列表:二级联动菜单 Select对象的常用属性 options[]:返回所有option组成的一个数组: name:名称 value:option的value的值 length:设置或读取opti ...

  9. Airbase-ng帮助

    Airbase-ng 1.2 rc2 - (C) 2008-2014 Thomas d'Otreppe  Original work: Martin Beck  http://www.aircrack ...

  10. Android USB Connections Explained: MTP, PTP, and USB Mass Storage

    Android USB Connections Explained: MTP, PTP, and USB Mass Storage Older Android devices support USB ...