在我们使用Dialog时,如果需要用到很多自己设计的控件,虽然可以让弹出框显示出我们需要的界面,但却无法找到地方完成控制代码的编写,如何解决这个问题呢,我们可以将Activity伪装成Dialog弹出框,这样即显示了界面,在Activity里写控制代码也是大家的拿手好戏了,现在我就来抛砖引玉说说简单的实现吧。


首先,问题的关键在MainActivity里的一句android:theme="@android:style/Theme.Dialog",这就是Activity的Dialog风格。

我们先创建一个main.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="fill_parent"    ><TextView  android:id="@+id/showString"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="在这里显示dialog里输入的数字:"    />  <Button   android:id="@+id/openButton"android:text="点此打开Dialog"android:layout_width="fill_parent"android:layout_height="wrap_content"  />  </LinearLayout>

再创建一个textdialog.xml, 内容如下

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="match_parent"  android:layout_height="match_parent"><EditText android:id="@+id/et"android:layout_width="fill_parent"android:layout_height="wrap_content"/>   <Button android:id="@+id/returnButton"android:text="请输入字符"android:layout_width="fill_parent"android:layout_height="wrap_content"/> </LinearLayout>


现在在MainActivity里写下如下代码,都是很基本的代码,相信大家都能看懂  

public class MainActivity extends Activity {private Button openButton;private TextView showString;    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                openButton = (Button)findViewById(R.id.openButton);        showString = (TextView)findViewById(R.id.showString);                openButton.setOnClickListener(new OnClickListener() {public void onClick(View v) {//这里用到了返回试Activity的基本用法,因为和主题无关,就不多解释了Intent i = new Intent(MainActivity.this, testDialog.class);startActivityForResult(i, 0);}});            }//利用返回试Activity接收输入的数据并显示,证明我们的Dialog式的Activity确实可以完成数据的处理protected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);//取出字符串Bundle bundle = data.getExtras();String str = bundle.getString("str");showString.setText(str);}}

下面是testDialog的编程,你可以看出这个Dialog和正常的Activity就没什么区别,但它最后 确实可以像Dialog一样弹出
ublic class testDialog extends Activity{private Button returnButton;private EditText inputEditor;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.textdialog);returnButton = (Button)findViewById(R.id.returnButton);inputEditor = (EditText)findViewById(R.id.et);//和前面一样,只是用到了返回式Activity的基本方法,虽然这里已经是个Dialog了,但却和普通Activity无异returnButton.setOnClickListener(new OnClickListener() {public void onClick(View v) {String input = inputEditor.getText().toString();Intent i = new Intent(testDialog.this, MainActivity.class);Bundle b = new Bundle();b.putString("str", input);i.putExtras(b);testDialog.this.setResult(RESULT_OK, i);testDialog.this.finish();}});}}

最后的亮点,设置Activity的Dialog风格,在 MainActivity里注册下第二个Activity吧,别完了风格设置哦

<activity android:name=".testDialog"  android:label="这是一个Activity变成的Dialog"  android:theme="@android:style/Theme.Dialog"></activity>

好了,你可以运行一下了,如果正常,你将看到和我一样的结果




注:这篇博文是第二届 Google 暑期大学生博客分享大赛 - 2011 Android 成长篇的参赛作品,如果你喜欢它,请支持我,谢谢!

更多相关文章

  1. Android窗口管理服务WindowManagerService显示窗口动画的原理分
  2. 实用的Log打印类封装,助尼快速定位源码(android)
  3. Android(安卓)NDK开发技术与技巧总结与心得
  4. android adb 流程原理代码分析(一)
  5. android NDK开发在本地C/C++源代码中设置断点单步调试详细教程
  6. MTK平台修改Bootloader源代码,让Android设备一通电就自动开机
  7. 实现ListView的条目下自动隐藏显示Button的方法
  8. 【理论】【转】App工程结构搭建:几种常见Android代码架构分析
  9. Android(安卓)Library 工程实现模块复用 (代码及资源文件)

随机推荐

  1. Android中Dialog
  2. Android平台架构介绍和源码下载
  3. Android网格布局(GirdView)学习
  4. android:ellipsize属性
  5. Android开发进阶之NIO非阻塞包(三)
  6. Android应用程序的六个组成部分
  7. Android中的动画研究
  8. Qt on Android: Qt 5.3.0 发布,针对 Andro
  9. Android入门(4) 布局、JUnit、日志与数据
  10. 华为发布全球首款Android(安卓)3.2平板电