Android 中类似ModelWindow的一个实现
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的一个实现的更多相关文章
- Android中关于XML的一个小问题——使用XML输出“<”错误的问题
在 XML 中,有 5 个预定义的实体引用: < < 小于 > > 大于 & & 和号 ' ' 单引号 " " 引号 注释 ...
- Android中使用shape制作一个旋转的progressbar
public class ZNtestResActivity extends Activity { @Override public void onCreate(Bundle savedInstanc ...
- [转]Android中Xposed框架篇—利用Xposed框架实现拦截系统方法
一.前言 关于Xposed框架相信大家应该不陌生了,他是Android中Hook技术的一个著名的框架,还有一个框架是CydiaSubstrate,但是这个框架是收费的,而且个人觉得不怎么好用,而Xpo ...
- [转载] 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 ...
- android中的一些问题
1. Android dvm的进程和Linux的进程, 应用程序的进程是否为同一个概念 DVM指dalivk的虚拟机.每一个Android应用程序都在它自己的进程中运行,都拥有一个独立的Dalvik虚 ...
- Android中的Surface和SurfaceView
一.什么是Surface 简单的说Surface对应了一块屏幕缓冲区,每个window对应一个Surface,任何View都要画在Surface的Canvas上(后面有原因解释).传统的view共享一 ...
- android中SELINUX规则分析和语法简介【转】
本文转载自:https://blog.csdn.net/LoongEmbedded/article/details/62430039 1. SELINUX是可以理解为一种Android上面的安全机制, ...
- android中SELINUX规则分析和语法简介
1. SELINUX是可以理解为一种android上面的安全机制,是有美国国家安全局和一些公司设计的一个针对linux的安全加强系统我们可以通过配置SELINUX的相关policy,来定制自己的手机的 ...
- CocosCreator反射在Android中的使用
CocosCreator反射在Android中的使用 新建一个CocosCreator项目,然后点击构建 构建完成之后,即可用AndroidStudio打开构建的项目 使用AndroidStudio打 ...
随机推荐
- mac 连接mysql提示 Warning: mysqli::real_connect(): (HY000/2002): No such file or directory
mac 连接mysql的时候提示 Warning: mysqli::real_connect(): (HY000/2002): No such file or directory [说明1]MAC下M ...
- Linux配置防火墙 开启80端口
vi /etc/sysconfig/iptables -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT(允许80端口通过防火 ...
- Linux的三种特殊权限
1.Suid Set位权限 ●对文件以文件的拥有者身份执行文件 ●对目录无影响 权限设置: ●suid=u+s 2.Sgid Set位权限 ●对文件以文件的组身份执行文件 ●对目录在目录中最新创建的文 ...
- 配置Java环境-20160613
http://jingyan.baidu.com/article/870c6fc33e62bcb03fe4be90.html 1.安装JDK,参照目录 在D:\Program Files\ec ...
- SharePoint 2013 开发——APP安全模型
博客地址:http://blog.csdn.net/FoxDave 除非开启了SharePoint网站的匿名访问,否则对于入站的请求,必须要有一个身份验证的过程(Authentication),这个 ...
- HTML--5 JavaScript
一.JavaScript简介 1.JavaScript是个什么东西? 它是个脚本语言,需要有宿主文件,它的宿主文件是HTML文件. 2.它与Java什么关系? 没有什么直接的联系,Java是Sun公司 ...
- 关于高并发的aotomic
AtomicInteger线程安全的根源,熟悉并发的同学一定知道在java中处理并发主要有两种方式: 1,synchronized关键字,这个大家应当都各种面试和笔试中经常遇到. 2,volatile ...
- 简单的php Mysql类(查询 删除 更新)
php Mysql类一般都包括了几乎我们常用的数据库操作方法,这里只提供了查询 删除 更新三种操作,算不是很全只是一个简单的数据库查询类了. 代码如下 复制代码 class mysql { ...
- 知道创宇CTO杨冀龙:网络安全人才决定行业格局
去年的时候,我曾经在团队内部分享了一个“网络安全人才迁徙图”,被圈子里的同行广为传播.几乎所有行业的安全人士都表示:国内网络安全行业人才资源情况并不乐观——有着10年以上网络安全行业经验的核心安全人才 ...
- 集合set的使用
将无序对象储存在集合中 集合是类似于数组的一组对象,只是其中包含的项目是无序的(而数组是有序的).您通过枚举集合中的对象,或者将过滤器或测试应用到集合,来随机访问集合中的对象(使用 anyObject ...