带输入框的Dialog自动弹出软键盘

自定义Dialog的时候,透明背景,添加输入框,自动弹出软键盘且不能遮盖住输入框等,添加如下样式解决:

<!--不带输入框-->  
<style name="dialog" parent="@android:style/Theme.Holo.Light.Dialog">  
    <item name="android:windowFrame">@color/transparent</item><!-- 边框 -->  
    <item name="android:windowIsFloating">true</item><!-- 是否浮现在activity之上 -->  
    <item name="android:windowIsTranslucent">false</item><!-- 半透明 -->  
    <item name="android:windowNoTitle">true</item><!-- 无标题 -->  
    <item name="android:windowBackground">@color/transparent</item><!-- 自己想要的背景 -->  
    <item name="android:backgroundDimEnabled">true</item><!-- 模糊 -->  
</style>  

<!--带输入框-->  
<style name="inputDialog" parent="@android:style/Theme.Holo.Light.Dialog">  
    <item name="android:windowFrame">@color/transparent</item><!-- 边框 -->  
    <item name="android:windowIsFloating">true</item><!-- 是否浮现在activity之上 -->  
    <item name="android:windowIsTranslucent">false</item><!-- 半透明 -->  
    <item name="android:windowNoTitle">true</item><!-- 无标题 -->  
    <item name="android:windowBackground">@color/transparent</item><!-- 自己想要的背景 -->  
    <item name="android:backgroundDimEnabled">true</item><!-- 模糊 -->  
    <item name="android:windowSoftInputMode">stateAlwaysVisible</item><!--显示软件盘-->  
</style>

样式具体使用:

    LinearLayout rootView = (LinearLayout) LayoutInflater.from(builder.mActivity).inflate(R.layout.share_dialog, null);     
    initViews(rootView);            
    initListener();                
    mDialog = new Dialog(builder.mActivity, R.style.ShareDialogTheme);      //这里创建dialog并应用我们自定义的主题
    mDialog.setContentView(rootView);                                      
    Window dialogWindow = mDialog.getWindow();  
    dialogWindow.setGravity(Gravity.BOTTOM);                               
    dialogWindow.setWindowAnimations(R.style.DialogAnimStyle);             
    WindowManager.LayoutParams lp = dialogWindow.getAttributes();

    //设置宽高
    lp.width = (int) getResources().getDisplayMetrics().widthPixels;  
    rootView.measure(0, 0);
    lp.height = rootView.getMeasuredHeight();                               
    lp.dimAmount = 0.75f;                                            //半透明背景的灰度 在0.0f和1.0f之间,0.0f完全不暗,1.0f全暗
    dialogWindow.setAttributes(lp);
    mDialog.setCancelable(true);
    mDialog.show();
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,897评论 22 665
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,432评论 25 708
  • 我们所理解的世界是我们所能感知的世界。笑来老师说过一个人能成长到多高的程度,就看他脑袋里有多少清晰,正确的概念。 ...
    三七糖阅读 592评论 0 0
  • 日剧《四重奏》又名《冯巩中提琴教学》,当中有句经典台词:“人生有三道,升道,降道,还有没想到。”这也是许多人在...
    WonderWe阅读 353评论 0 0
  • 你说过 你经常在想 站在这个世界 到底是来做什么的 哈哈 怪人一个 对不对 当时心里会有一种奇怪的排斥感 莫名其妙...
    喛萱阅读 504评论 0 0