android弹出对话框

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值