我试图找到我的问题的解决方案,有 1 个帖子有同样的问题,但没有得到回答(这里)
我创建了一个带有editTextView 的alertDialog。当我调用对话框时,键盘不会显示。我发现了一些确实显示键盘的代码,但只在 alertDialog 后面。
我的代码:(我已经注释掉了我的一些尝试)
package com.moyoweb.winescanner.dialogs;
import java.util.Calendar;
import com.moyoweb.winescanner.JSONParser;
import com.moyoweb.winescanner.R;
import com.moyoweb.winescanner.User;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
public class AddReviewDialog extends AlertDialog{
private Context context;
public AddReviewDialog(Context context) {
super(context);
this.setCanceledOnTouchOutside(true);
this.context=context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.dialog_add_review);
getWindow().setBackgroundDrawableResource(R.color.not_transparent);
final EditText etAddReview = (EditText)this.findViewById(R.id.dialog_add_review_edittext);
// InputMethodManager imm =
// (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
//// imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
// etAddReview.requestFocus();
//
// etAddReview.setFocusable(true);
// etAddReview.requestFocus();
// InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
// mgr.hideSoftInputFromWindow(etAddReview.getWindowToken(), 0);
etAddReview.requestFocus();
etAddReview.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
imm.showSoftInput(etAddReview, InputMethodManager.SHOW_FORCED);
}
},500);
// InputMethodManager imm = (InputMethodManager)
// context.getSystemService(Context.INPUT_METHOD_SERVICE);
// // For SHOW_FORCED
// imm.showSoftInput ( etAddReview, InputMethodManager.SHOW_FORCED);
// setOnShowListener(new OnShowListener() {
// @Override
// public void onShow(DialogInterface arg0) {
// // TODO Auto-generated method stub
// InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
// imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
// }
// });
Button btnSubmitReview = (Button)this.findViewById(R.id.dialog_btn_add_review);
this.setTitle("Adding a review");
final User us= new User();
btnSubmitReview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JSONParser.getInstance().addReviewWineToJSON(Calendar.getInstance().getTimeInMillis(),us.getUserID(), etAddReview.getText().toString());
dismiss();
}
});
}
}