zoukankan      html  css  js  c++  java
  • [转]如何利用Activity的Dialog风格完成弹出框设计

    本文转自:http://blog.csdn.net/tanjunjie621/article/details/6689639

     

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


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

    我们先创建一个main.xml,内容如下

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7. <TextView    
    8.     android:id="@+id/showString"  
    9.     android:layout_width="fill_parent"   
    10.     android:layout_height="wrap_content"   
    11.     android:text="在这里显示dialog里输入的数字:"  
    12.     />  
    13.   <Button   
    14.     android:id="@+id/openButton"  
    15.     android:text="点此打开Dialog"  
    16.     android:layout_width="fill_parent"  
    17.     android:layout_height="wrap_content"  
    18.   />    
    19. </LinearLayout>  


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

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout  
    3.   xmlns:android="http://schemas.android.com/apk/res/android"  
    4.   android:orientation="vertical"  
    5.   android:layout_width="match_parent"  
    6.   android:layout_height="match_parent">  
    7. <EditText   
    8.     android:id="@+id/et"  
    9.     android:layout_width="fill_parent"  
    10.     android:layout_height="wrap_content"  
    11.     />     
    12. <Button   
    13.     android:id="@+id/returnButton"  
    14.     android:text="请输入字符"  
    15.     android:layout_width="fill_parent"  
    16.     android:layout_height="wrap_content"  
    17.     />      
    18. </LinearLayout>  



    1. </pre>现在在MainActivity里写下如下代码,都是很基本的代码,相信大家都能看懂<p></p><p><span style="font-size:16px"><span style="font-size:16px"></span></span></p><pre name="code" class="java">public class MainActivity extends Activity {  
    2.       
    3.     private Button openButton;  
    4.     private TextView showString;  
    5.       
    6.     public void onCreate(Bundle savedInstanceState) {  
    7.         super.onCreate(savedInstanceState);  
    8.         setContentView(R.layout.main);  
    9.           
    10.         openButton = (Button)findViewById(R.id.openButton);  
    11.         showString = (TextView)findViewById(R.id.showString);  
    12.           
    13.         openButton.setOnClickListener(new OnClickListener() {  
    14.               
    15.         public void onClick(View v) {  
    16.                 //这里用到了返回试Activity的基本用法,因为和主题无关,就不多解释了  
    17.                 Intent i = new Intent(MainActivity.this, testDialog.class);  
    18.                 startActivityForResult(i, 0);  
    19.             }  
    20.         });  
    21.           
    22.     }  
    23.       
    24.     //利用返回试Activity接收输入的数据并显示,证明我们的Dialog式的Activity确实可以完成数据的处理  
    25.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    26.         super.onActivityResult(requestCode, resultCode, data);  
    27.         //取出字符串  
    28.         Bundle bundle = data.getExtras();  
    29.         String str = bundle.getString("str");  
    30.         showString.setText(str);  
    31.     }  
    32. }  


    下面是testDialog的编程,你可以看出这个Dialog和正常的Activity就没什么区别,但它最后确实可以像Dialog一样弹出

    1. ublic class testDialog extends Activity{  
    2.       
    3.     private Button returnButton;  
    4.     private EditText inputEditor;  
    5.       
    6.     protected void onCreate(Bundle savedInstanceState) {  
    7.         super.onCreate(savedInstanceState);  
    8.         setContentView(R.layout.textdialog);  
    9.           
    10.         returnButton = (Button)findViewById(R.id.returnButton);  
    11.         inputEditor = (EditText)findViewById(R.id.et);  
    12.           
    13.         //和前面一样,只是用到了返回式Activity的基本方法,虽然这里已经是个Dialog了,但却和普通Activity无异   
    14.         returnButton.setOnClickListener(new OnClickListener() {  
    15.             public void onClick(View v) {  
    16.                 String input = inputEditor.getText().toString();  
    17.                 Intent i = new Intent(testDialog.this, MainActivity.class);  
    18.                 Bundle b = new Bundle();  
    19.                 b.putString("str", input);  
    20.                 i.putExtras(b);  
    21.                 testDialog.this.setResult(RESULT_OK, i);  
    22.                 testDialog.this.finish();  
    23.             }  
    24.         });  
    25.     }  
    26. }  


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

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


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



     

  • 相关阅读:
    pixijs shader 制作百叶窗效果
    pixijs shader 贴图溶解效果教程
    shadertoy使用教程
    pixijs shader教程
    glsl shader简明教程系列1
    javascript判断mp3是否播放完
    wxWidgets Tutorial
    NYOJ 214 最长上升子序列nlogn
    解决codeforces访问慢的问题
    dp 斯特林数 HDU2512一卡通大冒险
  • 原文地址:https://www.cnblogs.com/freeliver54/p/2364652.html
Copyright © 2011-2022 走看看