我試圖在第一次打開應用程序時從用戶處獲得一個值。我正在充氣AlertDialog以獲取用戶的價值。儘管我可以看到光標閃爍,但我無法將任何文本輸入到EditText中。無法編輯AlertDialog中的EditText
在MainActivity
SharedPreferences sharedPreferences = getSharedPreferences("hasRunBefore", 0);
boolean hasRun = sharedPreferences.getBoolean("hasRun", false);
if (!hasRun) {
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putBoolean("hasRun", true);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater layoutInflater = LayoutInflater.from(this);
final View dialogView = layoutInflater.inflate(R.layout.alertdialog, null);
final EditText editText = (EditText) dialogView.findViewById(R.id.alert_dialog_editText);
builder.setView(dialogView)
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
regNum = editText.getText().toString();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
edit.putString("registerNumber", regNum);
edit.apply();
TextView textView = (TextView) findViewById(R.id.regNum_textView);
textView.setText(regNum);
} else {
//code if the app HAS run before
TextView textView = (TextView) findViewById(R.id.regNum_textView);
textView.setText(regNum);
}
代碼中的佈局文件是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/alertDialog"
>
<TextView
android:id="@+id/alert_dialog_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Your Register Number"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/alert_dialog_editText"
android:hint="Type your register number here"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_below="@+id/alert_dialog_textview"
/>
</RelativeLayout>
我在做什麼錯?
什麼時候你在寫什麼光標在移動? – Tara
@塔拉不,光標不移動。 – thatrockbottomprogrammer
宣佈全球ET而不是最終的本地變量 – Tara