2017-04-03 7 views
1

初めてアプリケーションを開いたときに値を取得しようとしています。私は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> 

私が間違って何をやっているのですか?

+0

あなたが何かを書いているときにカーソルが動いていますか? – Tara

+0

@Taraいいえ、カーソルは移動しません。 – thatrockbottomprogrammer

+0

最終ローカル変数の代わりにETをグローバルに宣言する – Tara

答えて

0

このコードを試してみてください。

// get yourView.xml view 
       LayoutInflater li = LayoutInflater.from(context); 
       View promptsView = li.inflate(R.layout.yourView, null); 

       AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
         context); 

       // set prompts.xml to alertdialog builder 
       alertDialogBuilder.setView(promptsView); 

       final EditText userInput = (EditText) promptsView 
         .findViewById(R.id.editTextDialogUserInput); 

       // set dialog message 
       alertDialogBuilder 
        .setCancelable(false) 
        .setPositiveButton("OK", 
         new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog,int id) { 
         // get user input and set it to result 
         // edit text 
         result.setText(userInput.getText()); 
         } 
         }) 
        .setNegativeButton("Cancel", 
         new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog,int id) { 
         dialog.cancel(); 
         } 
         }); 

       // create alert dialog 
       AlertDialog alertDialog = alertDialogBuilder.create(); 

       // show it 
       alertDialog.show(); 
1

この方法を使用してみてください:

public void showEditTextDialog() 
{ 

    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); 

    // Keyboard 
    final InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); 

    // Auto show keyboard 
    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean isFocused) { 

      if (isFocused) 
      { 
       imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 
      } 
     } 
    }); 

    builder.setView(dialogView) 
      .setPositiveButton("Okay", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 

        regNum = editText.getText().toString(); 
        Log.d("STACKOVERFLOW", "Registration number: " + regNum); 

        // Hide keyboard 
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); 
       } 
      }) 
      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 

        // Hide keyboard 
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); 

        dialog.cancel(); 
       } 
      }); 

    AlertDialog alertDialog = builder.create(); 
    alertDialog.show(); 
} 

OUTPUT:solutiものの

enter image description here

1

Ferdous Ahamedさんが投稿したところでは、最初は自分のコードに問題はありませんでした。私はアプリケーションでいくつかの権限を呼び出していましたが、適切に設定していませんでした。何が起こったのは、アプリケーションが開かれたときに、許可ダイアログにフォーカスが置かれたことです。そのダイアログを閉じると、フォーカスが、正しく構成されなかった2番目のアクセス権ダイアログに変わりました。したがって、フォーカスはバックグラウンドに送られ、無限にユーザーの入力を待っていました。

コードは、アクセス許可を修正した後に機能し始めました。私を助けてくれた皆様、おかげさまで感謝します。当初

権限コード:

/* Ensures Bluetooth is available on the device and it is enabled. If not, 
      displays a dialog requesting user permission to enable Bluetooth.*/ 
      if(!bluetoothAdapter.isEnabled()) 
      { 
       Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
       startActivityForResult(enableBtIntent, 1); 
      } 

      /*Getting Fine Location Access from the User. For odd reasons, the device won't scan if the location access isn't granted by the user.*/ 
       if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) 
         != PackageManager.PERMISSION_GRANTED) { 
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, 
          android.Manifest.permission.ACCESS_FINE_LOCATION)) { 

         // Show an explanation to the user *asynchronously* -- don't block 
         // this thread waiting for the user's response! After the user 
         // sees the explanation, try again to request the permission. 

        } else { 

         // No explanation needed, we can request the permission. 

         ActivityCompat.requestPermissions(MainActivity.this, 
           new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 
           MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); 

        } 
       } 

     //Getting READ_PHONE_STATE from the User. 
      if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.READ_PHONE_STATE) 
        != PackageManager.PERMISSION_GRANTED) { 
       if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, 
         android.Manifest.permission.READ_PHONE_STATE)) { 

        // Show an explanation to the user *asynchronously* -- don't block 
        // this thread waiting for the user's response! After the user 
        // sees the explanation, try again to request the permission. 

       } else { 

        // No explanation needed, we can request the permission. 

        ActivityCompat.requestPermissions(MainActivity.this, 
          new String[]{android.Manifest.permission.READ_PHONE_STATE}, 
          MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); 

       } 
      } 

は、私はそれを変更するコード:(。単一のタイムタスクへのアクセス権を吸収合併は、この先にやったなら私の悪い。。)

/*Getting Fine Location Access from the User. For odd reasons, the device won't scan if the location access isn't granted by the user. 
     * Also getting the READ_PHONE_STATE permission in the same dialog*/ 
      if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) 
        != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) 
        != PackageManager.PERMISSION_GRANTED) 
      { 
       if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, 
         android.Manifest.permission.ACCESS_FINE_LOCATION)) { 

        // Show an explanation to the user *asynchronously* -- don't block 
        // this thread waiting for the user's response! After the user 
        // sees the explanation, try again to request the permission. 

       } else { 

        // No explanation needed, we can request the permission. 

        ActivityCompat.requestPermissions(MainActivity.this, 
          new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION , android.Manifest.permission.READ_PHONE_STATE}, 
          MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); 
       } 
      } 
関連する問題