alert_dialog.Xml代码
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- android:orientation="vertical"
- >
- <LinearLayout
- android:id="@+id/pop_layout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- android:orientation="vertical"
- android:layout_alignParentBottom="true"
- android:background="@drawable/btn_style_alert_dialog_background"
- >
- <Button
- android:id="@+id/btn_take_photo"
- android:layout_marginLeft="20dip"
- android:layout_marginRight="20dip"
- android:layout_marginTop="20dip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="拍照"
- android:background="@drawable/btn_style_alert_dialog_button"
- android:textStyle="bold"
- />
- <Button
- android:id="@+id/btn_pick_photo"
- android:layout_marginLeft="20dip"
- android:layout_marginRight="20dip"
- android:layout_marginTop="5dip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="从相册选择"
- android:background="@drawable/btn_style_alert_dialog_button"
- android:textStyle="bold"
- />
- <Button
- android:id="@+id/btn_cancel"
- android:layout_marginLeft="20dip"
- android:layout_marginRight="20dip"
- android:layout_marginTop="15dip"
- android:layout_marginBottom="15dip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="取消"
- android:background="@drawable/btn_style_alert_dialog_cancel"
- android:textColor="#ffffff"
- android:textStyle="bold"
- />
- </LinearLayout>
- </RelativeLayout>
第二步:创建SelectPicPopupWindow类继承Activity类并实现OnClickListener接口(可以不用在这里实现这个借口,根据自己需要和方便实现),其他代码实现跟编写常规Activity一样就OK,如下:
Java代码
- mport android.app.Activity;
- import android.os.Bundle;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.Toast;
- public class SelectPicPopupWindow extends Activity implements OnClickListener{
- private Button btn_take_photo, btn_pick_photo, btn_cancel;
- private LinearLayout layout;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.alert_dialog);
- btn_take_photo = (Button) this.findViewById(R.id.btn_take_photo);
- btn_pick_photo = (Button) this.findViewById(R.id.btn_pick_photo);
- btn_cancel = (Button) this.findViewById(R.id.btn_cancel);
- layout=(LinearLayout)findViewById(R.id.pop_layout);
- //添加选择窗口范围监听可以优先获取触点,即不再执行onTouchEvent()函数,点击其他地方时执行onTouchEvent()函数销毁Activity
- layout.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Toast.makeText(getApplicationContext(), "提示:点击窗口外部关闭窗口!",
- Toast.LENGTH_SHORT).show();
- }
- });
- //添加按钮监听
- btn_cancel.setOnClickListener(this);
- btn_pick_photo.setOnClickListener(this);
- btn_take_photo.setOnClickListener(this);
- }
- //实现onTouchEvent触屏函数但点击屏幕时销毁本Activity
- @Override
- public boolean onTouchEvent(MotionEvent event){
- finish();
- return true;
- }
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btn_take_photo:
- break;
- case R.id.btn_pick_photo:
- break;
- case R.id.btn_cancel:
- break;
- default:
- break;
- }
- finish();
- }
- }
第三步:编写MainActivity类,这里很简单就是点击启动刚才要实现窗口的MainActivity即可:
Java代码
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- TextView tv = (TextView) this.findViewById(R.id.text);
- //把文字控件添加监听,点击弹出自定义窗口
- tv.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- startActivity(new Intent(MainActivity.this,SelectPicPopupWindow.class));
- }
- });
- }
- }
第四步:这里要注意下AndroidManifest.xml对SelectPicPopupWindow的配置跟常规的不一样为该activity改添加android:theme属性,如下:
Xml代码
- <activity android:name=".SelectPicPopupWindow" android:theme="@style/MyDialogStyleBottom" />
第五步:这一步是实现本实例最重要的一部就是设置android:theme属性样式以实现本例所需要的效果,如下:
Xml代码
- <style name="AnimBottom" parent="@android:style/Animation">
- <item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
- <item name="android:windowExitAnimation">@anim/push_bottom_out</item>
- </style>
- <style name="MyDialogStyleBottom" parent="android:Theme.Dialog">
- <item name="android:windowAnimationStyle">@style/AnimBottom</item>
- <item name="android:windowFrame">@null</item>
- ;!-- 边框 -->
- <item name="android:windowIsFloating">true</item>
- ;!-- 是否浮现在activity之上 -->
- <item name="android:windowIsTranslucent">true</item>
- ;!-- 半透明 -->
- <item name="android:windowNoTitle">true</item>
- ;!-- 无标题 -->
- <item name="android:windowBackground">@android:color/transparent</item>
- ;!-- 背景透明 -->
- <item name="android:backgroundDimEnabled">true</item>
- ;!-- 模糊 -->
- </style>
第六步:在贴出弹出和销毁时的动画效果代码:
push_bottom_in.xml
Xml代码
- <?xml version="1.0" encoding="utf-8"?>
- <!-- 上下滑入式 -->
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <translate
- android:duration="200"
- android:fromYDelta="100%p"
- android:toYDelta="0"
- />
- </set>
push_buttom_out.xml
Xml代码
- <?xml version="1.0" encoding="utf-8"?>
- <!-- 上下滑出式 -->
- <set xmlns:android="http://schemas.android.com/apk/res/android" >
- <translate
- android:duration="200"
- android:fromYDelta="0"
- android:toYDelta="50%p" />
- </set>
注意:这两个xml需要放在res/anim的anim文件夹下
时间: 2024-11-08 05:44:12