日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android开发:利用Activity的Dialog风格完成弹出框设计

發(fā)布時(shí)間:2025/3/11 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android开发:利用Activity的Dialog风格完成弹出框设计 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn):http://www.linuxidc.com/Linux/2011-08/41933.htm

在我們使用Dialog時(shí),如果需要用到很多自己設(shè)計(jì)的控件,雖然可以讓彈出框顯示出我們需要的界面,但卻無(wú)法找到地方完成控制代碼的編寫,如何解決這個(gè)問(wèn)題呢,我們可以將Activity偽裝成Dialog彈出框,這樣即顯示了界面,在Activity里寫控制代碼也是大家的拿手好戲了,現(xiàn)在我就來(lái)拋磚引玉說(shuō)說(shuō)簡(jiǎn)單的實(shí)現(xiàn)吧。

首先,問(wèn)題的關(guān)鍵在MainActivity里的一句?Android:theme="@android:style/Theme.Dialog",這就是Activity的Dialog風(fēng)格。

我們先創(chuàng)建一個(gè)main.xml,內(nèi)容如下

  • <?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里輸入的數(shù)字:"??
  • ????/>??
  • ??<Button???
  • ????android:id="@+id/openButton"??
  • ????android:text="點(diǎn)此打開(kāi)Dialog"??
  • ????android:layout_width="fill_parent"??
  • ????android:layout_height="wrap_content"??
  • ??/>????
  • </LinearLayout>??
  • 再創(chuàng)建一個(gè)textdialog.xml, 內(nèi)容如下
  • <?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="請(qǐng)輸入字符"??
  • ????android:layout_width="fill_parent"??
  • ????android:layout_height="wrap_content"??
  • ????/>??????
  • </LinearLayout>??

  • 現(xiàn)在在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的基本用法,因?yàn)楹椭黝}無(wú)關(guān),就不多解釋了??
  • ????????????????Intent?i?=?new?Intent(MainActivity.this,?testDialog.class);??
  • ????????????????startActivityForResult(i,?0);??
  • ????????????}??
  • ????????});??
  • ??????????
  • ????}??
  • ??????
  • ????//利用返回試Activity接收輸入的數(shù)據(jù)并顯示,證明我們的Dialog式的Activity確實(shí)可以完成數(shù)據(jù)的處理??
  • ????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的編程,你可以看出這個(gè)Dialog和正常的Activity就沒(méi)什么區(qū)別,但它最后 確實(shí)可以像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的基本方法,雖然這里已經(jīng)是個(gè)Dialog了,但卻和普通Activity無(wú)異???
  • ????????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();??
  • ????????????}??
  • ????????});??
  • ????}??
  • }??
  • 最后的亮點(diǎn),設(shè)置Activity的Dialog風(fēng)格,在 MainActivity里注冊(cè)下第二個(gè)Activity吧,別完了風(fēng)格設(shè)置哦
  • <activity?android:name=".testDialog"??
  • ??????????????android:label="這是一個(gè)Activity變成的Dialog"??
  • ??????????????android:theme="@android:style/Theme.Dialog"??
  • ????????></activity>??
  • 好了,你可以運(yùn)行一下了,如果正常,你將看到和我一樣的結(jié)果


    總結(jié)

    以上是生活随笔為你收集整理的Android开发:利用Activity的Dialog风格完成弹出框设计的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。