Android 中类似ModelWindow的一个实现

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

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

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

的例子。

代码

MainActivity.java

 1 package com.example.testpopactivity;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.view.View.OnClickListener;
 8 import android.widget.Button;
 9 import android.widget.TextView;
10 
11 public class MainActivity extends Activity implements OnClickListener {
12 
13     @Override
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.activity_main);
17         
18         Button btnYes = (Button) findViewById(R.id.btnPopup);
19         btnYes.setOnClickListener(this);
20         
21     }
22 
23     @Override
24     public void onClick(View view) {
25         switch (view.getId()) {
26         case R.id.btnPopup:
27             Intent intent = new Intent(MainActivity.this, PopupActivity.class);            
28             startActivityForResult(intent,R.layout.activity_popup);
29             break;
30         default:
31             break;
32         }
33         
34     }
35     
36     @Override
37     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
38         switch (requestCode) {
39         case R.layout.activity_popup:
40             String input = (data ==  null? "" : data.getStringExtra("input"));
41             if(!"".equals(input)) {
42                 TextView tvLabel = (TextView)findViewById(R.id.tvLabel);
43                 tvLabel.setText(input);
44             }
45             break;
46         default:
47             break;
48         }
49     }
50 
51 }

PopupActivity.java

 1 package com.example.testpopactivity;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.view.View.OnClickListener;
 8 import android.widget.EditText;
 9 
10 public class PopupActivity extends Activity implements OnClickListener {
11 
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_popup);
16         
17         setFinishOnTouchOutside(false); // to set this could prevent window from closing by touching transparent part. 
18         findViewById(R.id.btnOk).setOnClickListener(this);
19         
20     }
21 
22     @Override
23     public void onClick(View view) {
24         Intent intent = new Intent();
25         switch (view.getId()) {
26         case R.id.btnOk:
27             String input = ((EditText) findViewById(R.id.etInput)).getText().toString();
28             intent.putExtra("input", input);
29             setResult(RESULT_OK, intent);
30             finish();
31             break;
32         default:
33             break;
34         }
35     }
36 
37 }

activity_main.xml

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:gravity="center"
 6     tools:context=".MainActivity" >
 7 
 8     <TextView
 9         android:id="@+id/tvLabel"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:text="@string/hello_world" />
13     
14     <Button
15         android:id="@+id/btnPopup"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:layout_below="@id/tvLabel"
19         android:text="@string/btn_popup" />
20 
21 </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

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.testpopactivity"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="14"
 9         android:targetSdkVersion="14" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name="com.example.testpopactivity.MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25         <activity
26             android:name="com.example.testpopactivity.PopupActivity"
27             android:label="@string/title_activity_popup"
28             android:theme="@android:style/Theme.Dialog"
29              >
30         </activity>
31     </application>
32 
33 </manifest>

效果如图:

posted on 2014-07-21 18:07  扭头撞到墙  阅读(613)  评论(0编辑  收藏  举报