安卓开发 自定义Dialog的问题总结之---Title背景为黑问题、edittext无法弹出软键盘

前几天,电脑出现问题,开机不断重启,怎么搞都木有用。于是乎狠下心来重装。那么问题来了,以前的项目呢?日了狗了,项目只备份到7月1号的,接下来只能慢慢写回来吧,之前dialog写得很顺利,白底蓝线黑字。但是今天写的话,出现了问题。下面来一一总结。
**

一、一开始想到的是利用AlertDialog来实现。

代码如下:

mBuilder = new AlertDialog.Builder(getActivity());
AlertDialog alertDialog = builder.create();

LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT-100, LayoutParams.WRAP_CONTENT);//设置dialog的宽高

View view = LayoutInflater.from(getActivity()).inflate(R.layout.alertdialog_setserver, null, false);
editText_alertDialog_setServerIP = (EditText)view.findViewById(R.id.editText_alertDialog_setServerIP);
mBuilder.setView(view);
mBuilder.setPositiveButton("确定",new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                Constant.LINUXSERVERIPANDCLIENT = editText_alertDialog_setServerIP.getText().toString();
                Constant.setServerIP();
                Toast.makeText(getActivity(), Constant.DEMOISREGISTER, 3000).show();
            }});
        mBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                mBuilder = null;
        }});
mBuilder.setTitle("设置服务器IP");
mBuilder.show();

结果发现Title部分竟然是黑色的,黑色的,尼玛,有木有太丑。于是乎找各种资料,想去修改主题

public Builder(Context context, int theme) ;

确实有主题这个东西,可是系统的主题,你能确定它是做什么的?反正最后出现了各种奇怪的效果。

二、自己写一个自定义的Dialog 继承于AlertDialog

真心不能忍,不能忍怎么办呢? 自己写一个自定义的Dialog 继承于AlertDialog,问题又出现了,里面的EditText获取焦点竟然不能弹出软键盘。尼玛,这叫一个纠结。百度吧,都是坑,当然也有可能是方法过时了。反正不管怎么搞都出不来。

三、那就继承Dialog吧

这是代码:

public class SetServerDialog extends Dialog implements android.view.View.OnClickListener {
    private Context context;
    private EditText editText_alertDialog_setServerIP;
    private Button button_setServer_cancle;
    private Button button_serServer_ok;

    public SetServerDialog(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        init();
    }

    private void init(){
        View view = LayoutInflater.from(context).inflate(R.layout.alertdialog_setserver, null, false);
        setContentView(view);

        editText_alertDialog_setServerIP = (EditText) view.findViewById(R.id.editText_alertDialog_setServerIP);
        button_serServer_ok = (Button) view.findViewById(R.id.button_serServer_ok);
        button_setServer_cancle = (Button) view.findViewById(R.id.button_setServer_cancle);

        button_serServer_ok.setOnClickListener(this);
        button_setServer_cancle.setOnClickListener(this);
        editText_alertDialog_setServerIP.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
        Window dialogWindow = getWindow(); //获取Dialog的window,用来设置dialog的各种属性
        dialogWindow.setBackgroundDrawableResource(R.drawable.dialogcustom_background);

        WindowManager.LayoutParams layoutParams = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics();//获取屏幕尺寸
        layoutParams.width = (int) (d.widthPixels*0.8);
        dialogWindow.setSoftInputMode(layoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        dialogWindow.setAttributes(layoutParams);//设置属性



    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button_serServer_ok:
            Constant.LINUXSERVERIPANDCLIENT = editText_alertDialog_setServerIP.getText().toString();
            Constant.setServerIP();
            Toast.makeText(context, Constant.DEMOISREGISTER, 3000).show();
            cancel();
            break;
        case R.id.button_setServer_cancle:
            cancel();

            break;

        default:
            break;
        }
    }

结果问题又出来了,Dialog的Title部分还是黑色的,好吧,自定义的总好搞了吧。下面是在Fragment里面调用自定义Dialog。

final SetServerDialog dialog = new SetServerDialog(getActivity());
        dialog.setCancelable(false);
//      dialog.setTitle("设置服务器IP");
        int i = R.style.TextAppearance_AppCompat_Base_CompactMenu_Dialog;
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//非常关键的一个地方,这一行代码是用来取消Title效果的。
        dialog.show();

最后,问题解决了
这里写图片描述
**

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值