http://blog.csdn.net/pochuanpiao/archive/2011/04/28/6370573.aspx
目前学习到的android有三种方式弹出对话框:
1、AlertDialog
AlertDialog是非阻塞的,简单的记录一下创建的代码:
AlertDialog.Builder builder; AlertDialog alertDialog; Context mContext = getApplicationContext(); //mContext不可用 this.getContext()也行 builder = new AlertDialog.Builder(mContext); builder.setTitle("测试菜单"); //builder.setView(view); alertDialog = builder.create(); alertDialog.show();
new AlertDialog.Builder所需要的参数,经过测试和网上查找,发现是要Activity才可以的
所以最后方式了这种方式
2、PopupWindow
PopupWindow是阻塞的对话框,基本上满足要求,但是最后获取焦点,
进行onkey事件还是遇到了很多的问题。奇怪,还是把部分代码贴出来:
//创建PopupWindow
Context mContext = getApplicationContext();
LayoutInflater mflayout = getLayoutInflater();
View view = mflayout.inflate(R.layout.popupwindow, null);
PopupWindow mPopupWindow = new PopupWindow(view,
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
//mPopupWindow.setBackgroundDrawable(null);
//mPopupWindow.setFocusable(true); //获得焦点
//不知道为什么,最后还是不响应onkey事件,用下面的方法可以
view.setFocusableInTouchMode(true);//view是创建pop的子view
view.setOnKeyListener(new OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
return false;
}
});
3、采用service上面,通过intent,弹出一个Activity菜单
这个也是我所使用的办法,直接上参考代码吧:
<activity android:name="MainActivity" android:theme="@android:style/Theme.Dialog"> <intent-filter> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
Context mContext = getApplicationContext();
Intent activityIntent = new Intent(mContext, MainActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//activityIntent.putExtra("mCurKeyboard", mCurKeyboard);
Bundle mBundle = new Bundle();
mBundle.putInt("mCurKeyboard", 1);
activityIntent.putExtras(mBundle);
mContext.startActivity(activityIntent);
只是简单的一个思路,具体的细节,在实现的时候可以上网看看例子。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/pochuanpiao/archive/2011/04/28/6370573.aspx