2017-02-15 4 views
0

私は間違いを見つけるのに疲れました。私は間違いを見つけられませんでしたが、私はeditTextからテキストを取得していません。私はLockImmediately(本)と呼ばれるDialogのEditTextはテキストを返しません

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/layout_root" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:padding="10dp" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Enter PIN " 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<EditText 
    android:id="@+id/pwdValue" 
    android:layout_width="match_parent" 
    android:maxLength="4" 
    android:maxLines="1" 
    android:inputType="numberPassword" 
    android:layout_height="wrap_content"> 
</EditText> 
</LinearLayout> 

activity_pwd.xml

;:以下のコードを見てください。 MainActivity

public void LockImmediately(Context context) { 
    if (lock_app) { 
     View myview = LayoutInflater.from(context).inflate(R.layout.activity_pwd,null); 
     enterPWD = (EditText) myview.findViewById(R.id.pwdValue); 

     AlertDialog alertDialog = new AlertDialog.Builder(this).setCancelable(false) 
       .setView(R.layout.activity_pwd).setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         String user_text = enterPWD.getText().toString(); 
         Log.d("onCreate pws", " i "+user_text); 
         if (pwd.equals(user_text)) { 
          dialog.dismiss(); 
          Log.d("onCreate", "Work done"); 
         } else { 
          dialog.dismiss(); 
          MainActivity.this.finish(); 
         } 
        } 
       }).create(); 
     alertDialog.show(); 
    } 
} 

のonCreate法上の Log.d

02-15 23:15:30.840 29379-29379/com.developersqueen.wishlater D/onCreate: onCreate 
02-15 23:15:37.021 29379-29379/com.developersqueen.wishlater D/onCreate pws: i 
02-15 23:15:45.427 29379-29379/com.developersqueen.wishlater D/onCreate: onCreate 
02-15 23:15:49.026 29379-29379/com.developersqueen.wishlater D/onCreate pws: i 

あなたはその上にそのログを見ることができる私は、 "i" は、それが値を返さないだとCONCATENATEのEditText値を持っています..私はデータをクリアしようとしている、アプリケーションをunistalling、きれいな、常に同じ結果を構築する!任意の助けいただければ幸い..

+0

'alertDialog.findViewById(R.id.pwdValue).getText()。toString();'に 'enterPWD.getText()。toString();'を変更して、動作しているかどうか確認してください。 – Sanjeet

+0

不正な文字をコンパイルする時\ u200c –

+0

それから不正な文字を取り除き、 – Sanjeet

答えて

1

を試してみてください。

myviewを使用してEditTextのIDを検索しましたが、ダイアログからではなく、アクティビティからmyViewを取得しようとしています。

あなたはMYVIEWようにビューを設定した場合、あなたはenterPWDのEditTextからテキストを取得することができます。

AlertDialog alertDialog = new AlertDialog.Builder(this).setCancelable(false) 
       .setView(myview).setPositiveButton("OK", new DialogInterface.OnClickListener() { 
... 
} 

アクティビティとダイアログ自体のレイアウトを個別に作成する必要があります。両方に1つのレイアウトを使用しないでください。

+0

ありがとうございました!!これは魅力的に機能しました!^_ ^ –

0

あなたはViewを膨らませて、それをドロップし、

View myview = LayoutInflater.from(context).inflate(R.layout.activity_pwd,null);

は、ダイアログの View、その役に立たないインスタンスではありません。

レイアウトのIDをダイアログに渡します。ダイアログのIDは、.show()の独自のバージョンを展開します。 alertDialog.findViewById(R.id.pwdValue)を直接onClick(..)に使用してください。

+0

を試してみましょう –

+0

nullポインタ例外を投げる –

+0

どの行?私はちょうどそれを貼り付けコピーし、それは動作します – CrowsNet

0

あなたはAlertDialogにビューを設定しているとき、あなたはのEditTextのIDを発見したビューを設定する必要があります。この

public void LockImmediately(Context context) { 
    if (lock_app) { 
     AlertDialog alertDialog = new AlertDialog.Builder(this).setCancelable(false) 
       .setView(R.layout.activity_pwd).setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
          EditText enterPWD = (EditText) myview.findViewById(R.id.pwdValue); 
          String user_text = enterPWD.getText().toString(); 
          Log.d("onCreate pws", " i "+user_text); 
          if (pwd.equals(user_text)) { 
           dialog.dismiss(); 
           Log.d("onCreate", "Work done"); 
          } else { 
           dialog.dismiss(); 
           MainActivity.this.finish(); 
          } 
         } 
        }).create(); 
      alertDialog.show(); 
     } 
    } 
+0

あなたはどのような変更を行ったのですか? –

+0

あなたのレイアウトを削除して、クリックして内部のテキストを最初に編集します@appleappala –

関連する問題