我试图找到问题的解决方案,有1个帖子有同样的问题,但没有回答(here)
我创建了一个带有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();
}
});
}
}
答案 0 :(得分:2)
而不是alertdialog,您可以使用活动和清单集主题中的活动,如此
android:theme="@android:style/Theme.Dialog"
活动看起来像对话框