当我显示一个警告对话框,用户可以在editText中输入一定数量的玩家时,我找不到通过手机弹出键盘的方法。因此,当我在物理手机上运行应用程序时,我无法在editText中输入任何内容。这里也是我的Main和对话框类的代码:
主:
package com.example.joppe.firstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements
spelersDialog.spelersDialogListener {
private TextView textView_test;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView_test = (TextView) findViewById(R.id.textView_test);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openDialog();
}
});
}
public void openDialog() {
spelersDialog SpelersDialog = new spelersDialog();
SpelersDialog.show(getSupportFragmentManager(), "spelers Dialog");
}
@Override
public void applytexts(String number) {
textView_test.setText(number);
}
}
alertDialog:
package com.example.joppe.firstapp;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
public class spelersDialog extends AppCompatDialogFragment {
private EditText players;
private spelersDialogListener listener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_dialog, null);
builder.setView(view)
.setTitle("Number of players:")
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("apply", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String number = players.getText().toString();
listener.applytexts(number);
}
});
players = (EditText) view.findViewById(R.id.players);
return builder.create();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
listener = (spelersDialogListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() +
"must implement spelersDialogListener");
}
}
public interface spelersDialogListener {
void applytexts(String number);
}
}
答案 0 :(得分:0)
@Override
public void onResume() {
super.onResume();
//Show the keyboard
players.post(new Runnable()
{
@Override
public void run()
{
players.requestFocus();
InputMethodManager imm =
(InputMethodManager)etMeterValue.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null)
imm.showSoftInput(etMeterValue, InputMethodManager.SHOW_IMPLICIT);
}
});
}