在 Act1 上弹出 一个窗口,窗口内容为 Act2
Act1 source: 其中里面有个遍历 judgealert 用来做标示判断,目的是在oncreate 时 不执行 onresume
package t1.com; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class T1Activity extends Activity { /** Called when the activity is first created. */ private Button at; private boolean judgealert ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); judgealert = false; at = (Button)findViewById(R.id.at); at.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub judgealert = true; Intent intent = new Intent(); intent.setClass(T1Activity.this, AlertAct.class); T1Activity.this.startActivity(intent); } }); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); if(judgealert == true){ Toast.makeText(T1Activity.this, "弹出窗口关闭咯", Toast.LENGTH_LONG).show(); } judgealert = false; } }
Act1 layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/at" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="弹出" /> </LinearLayout>
Act2 source:
package t1.com; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.SeekBar; import android.widget.TextView; public class AlertAct extends Activity { /** Called when the activity is first created. */ private Button close; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.alert); close = (Button)findViewById(R.id.close); close.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub AlertAct.this.finish(); } }); } }
Act2 layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="hello.. i am alert!" /> <Button android:id="@+id/close" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="关闭" /> </LinearLayout>
在 Manifest 中 要给予 Act2 属性 android:theme="@android:style/Theme.Dialog"
另外 弹出 Act 也可以自定义 窗口属性 放在Act2 中
Dialog dialog = new Dialog(LoginAddSensor.this);
Window dialogWindow = dialog.getWindow();
WindowManager m = getWindowManager();
Display d = m.getDefaultDisplay();
WindowManager.LayoutParams p = getWindow().getAttributes();
p.width = (int)(d.getWidth()*0.8);
p.alpha = 0.8f;
dialogWindow.setAttributes(p);
-----------------------------------------------
** 解决弹出窗口去掉标题栏
先在 res/values/styles.xml 中新建 style
<?xml version="1.0" encoding="UTF-8"?> <resources> <style name="myDialogTheme" parent="android:Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">false</item> <item name="android:windowNoTitle">true</item><!--除去title--> <item name="android:windowContentOverlay">@null</item> <item name="android:backgroundDimEnabled">false</item> <item name="android:windowBackground">@null</item><!--除去背景色--> </style> </resources>
然后在 Manifest 中 的 Activity 加载 Theme
android:theme="@style/myDialogTheme" 即可
也可以在里面定义 字体颜色 如下通用 的 Activity Theme
<?xml version="1.0" encoding="UTF-8"?> <resources> <style name="globalActTheme" parent="android:Theme.Translucent.NoTitleBar"> <item name="android:textColor">#ff0000</item> </style> </resources>