安卓自定义Dialog中的EditText弹不出键盘,跟遮挡键盘的问题解决

//////////////////2016/03/13/////////////////////

/////////////////by xbw////////////////////////////

////////////////环境 eclipse////////////////////

先上两张图

第一张问题很明显,第二张是成功的图,

其实第一张是加了

 //getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 
        //     InputMethodManager imm = (InputMethodManager)
        //                     getSystemService(INPUT_METHOD_SERVICE);
        //                      imm.showSoftInput(layout, 0); //显示软键盘
        //                      imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); //显示软键盘

这段代码,自动弹出来的,结构被遮挡了,查了一番资料也没解决,一群里哥们提示了一句代码,结果成功了,爽,

 dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 
		dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

就是这么神奇,

下面把我整个showdialog方法贴出来吧;

public  void showLogoutDialog(final int charnum,final String title) {
		
		LayoutInflater layoutInflater =LayoutInflater.from(this);
		RelativeLayout layout = (RelativeLayout)layoutInflater.inflate(R.layout.change_mes_dialog, null );
		final Dialog dialog = new AlertDialog.Builder(MimeDetailActivity.this).create();
		dialog.show();
  	        dialog.getWindow().setContentView(layout);
  	        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 
		dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        //getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 
        //     InputMethodManager imm = (InputMethodManager)
        //                     getSystemService(INPUT_METHOD_SERVICE);
        //                      imm.showSoftInput(layout, 0); //显示软键盘
        //                      imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); //显示软键盘
                              
                      	
	    TextView tex=(TextView)layout.findViewById(R.id.dialog_text);
	    TextView tex1=(TextView)layout.findViewById(R.id.textView_title);
	    tex.setMovementMethod(ScrollingMovementMethod.getInstance());
	    tex1.setText("修改提示");
	    tex.setText(title);     
	    edit = (EditText) layout.findViewById(R.id.edit_caonimalebi);
	    edit.addTextChangedListener(myWatcher);  //监听edittext
	    charnum_tx=(TextView)layout.findViewById(R.id.charnum_tx);
         //确定按钮
         Button btnOK = (Button) layout.findViewById(R.id.dialog_ok);
         btnOK.setText("更新");
         btnOK.setOnClickListener(new OnClickListener() {         
           @Override 
           public void onClick(View v) {
        		if(edit.getText().length()>charnum){
        			Toast.makeText(MimeDetailActivity.this, "字数超限", Toast.LENGTH_SHORT).show();
        		}else{
        			new Up().execute();  
        			//dialog.dismiss();    
        	    }
           }
         });	        
         //btnOK.setClickable(btn);
         //关闭按钮
         ImageButton btnClose = (ImageButton) layout.findViewById(R.id.dialog_close);
         btnClose.setOnClickListener(new OnClickListener() {	          
           @Override
           public void onClick(View v) {
              dialog.dismiss();          
           }
         });
	}

中间还使用了对edittext的监听,来判断输入的字的个数,防止输入超限,,,,

这个监听呢,edit这还出来不少问题,
edit = (EditText) layout.findViewById(R.id.edit_caonimalebi);玩玩没想到忽略了layout,导致程序总是崩溃,唉,粗心了,

这个edittext的监听也贴出来

TextWatcher myWatcher = new TextWatcher(){
		@Override
		public void afterTextChanged(Editable s) {
			// TODO 自动生成的方法存根
			charnum_tx.setText("您还可以输入"+(charnum - edit.getText().length())+" \\ "+charnum);
		}
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO 自动生成的方法存根	
		}
		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO 自动生成的方法存根
		}
		};

,唉收工,累死了,谢谢好人。

  1. 想问一下。你这个提示窗口的layout怎么写的?,这个叉叉半个在外面我写了半天没成功,邮箱:mrkey.k@gmail.com,或者QQ8563487,谢谢大神

  2. 还有个问题,我用你这个方法。输入法只能把dialog顶到正好在edittext下面。不能把整个窗口顶起来。也就是没办法把确定取消露出来,为什么你这里可以整个dialog顶起来?

  3. 没解决。。。LayoutInflater layoutInflater = LayoutInflater.from(this); LinearLayout layout = (LinearLayout) layoutInflater.inflate(R.layout.dialog_comment, null); final Dialog commentDialog = new AlertDialog.Builder(this).create(); commentDialog.show(); commentDialog.getWindow().setContentView(layout); commentDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); commentDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); final EditText commentEditText = (EditText) layout.findViewById(R.id.et_comment_edittext); Button cancelButton = (Button) layout.findViewById(R.id.btn_set_font_cancel); Button sendButton = (Button) layout.findViewById(R.id.btn_set_font_send); cancelButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { commentDialog.dismiss(); } }); sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String comment = commentEditText.getText().toString(); if (!TextUtils.isEmpty(comment)) { sendComment(comment); commentDialog.dismiss(); } else { ProgressHUD.showInfo(NewsDetailActivity.this, “请输入评论内容”); } } }); // 自动弹出键盘 Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { InputMethodManager inputManager = (InputMethodManager) commentEditText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(commentEditText, 0); } }, 250);还是会遮挡