2013-05-06 97 views
0

我在爲Android編程應用newbee。對於我的應用程序,我需要在應用程序啓動後輸入密碼 .....迄今爲止這麼好。修改的EditText顏色AlertDialog將無法正常工作

但在喊我alertdialog輸入的文本我txtPIN不能被看到的而言,它總會有顏色作爲背景相同。我使用API​​ Lvl 17作爲目標API。

如果任何人都可以給我一個提示,我會很高興。

 if (dbManager.getToggle()==true){ 

     final EditText txtPIN = new EditText(this); 
     txtPIN.setTextColor(color.black); 
     txtPIN.setInputType(InputType.TYPE_CLASS_NUMBER); 

     AlertDialog.Builder alert = new AlertDialog.Builder(this,AlertDialog.THEME_HOLO_LIGHT); 
     alert.setTitle("PIN Security"); 
     alert.setMessage("Please enter PIN"); 
     alert.setView(txtPIN); 
     alert.setIcon(R.drawable.ic_launcher); 

     alert.setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       //comparison of PINs 
       if (txtPIN.getText().toString().equals(dbManager.getPIN()){ 
        Toast toast = Toast.makeText(getApplicationContext(), R.string.allowed, Toast.LENGTH_SHORT); 
        toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 150); 
        toast.show(); 

       } else { 
        Toast toast = Toast.makeText(getApplicationContext(), getResources().getString(R.string.denied) + "\n" + txtPIN.getText().toString() + "\n" + dbManager.getPIN(), Toast.LENGTH_SHORT); 
        toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 150); 
        toast.show(); 

        finish(); 
        System.exit(0); 
       } 

      } 
     }); 
+0

我覺得現在的問題是,'EditText'與創建應用程序'上下文'而不是'AlertDialog'。因此'EditText'不可能知道這個主題,因爲它不能猜測它將被放置在哪裏。嘗試通過調用'AlertDialog對話框= alert.create(先創建一個'AlertDialog')',然後創建您的編輯文字'最後的EditText txtPIN =新的EditText(dialog.getContext());'。 – cgogolin 2015-11-27 14:19:41

回答

0

像這樣創建EditText使用XML:

<EditText xmlns:andoird="http://schemas.android.com/apk/res/android" 
    android:id="@+id/edit_text" 
    android:inputType="number" 
    android:textColor="#000000" 
    android:OTHER_ATTRIBUTES_HERE/> 

,然後再製作AlertDialog像這樣:

AlertDialog.Builder alert = new AlertDialog.Builder(this,AlertDialog.THEME_HOLO_LIGHT); 
LayoutInflater inflater = getActivity().getLayoutInflater(); 
View v = inflater.inflate(R.layout.edit_text_xml_file, null)); 
    alert.setTitle("PIN Security"); 
    alert.setMessage("Please enter PIN"); 
    alert.setView(v); 
    alert.setIcon(R.drawable.ic_launcher); 
    EditText txtPIN = (EditText) v.findViewById(R.id.edit_text); 

    alert.setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      //comparison of PINs 
      if (txtPIN.getText().toString().equals(dbManager.getPIN()){ 
       Toast toast = Toast.makeText(getApplicationContext(), R.string.allowed, Toast.LENGTH_SHORT); 
       toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 150); 
       toast.show(); 

      } else { 
       Toast toast = Toast.makeText(getApplicationContext(), getResources().getString(R.string.denied) + "\n" + txtPIN.getText().toString() + "\n" + dbManager.getPIN(), Toast.LENGTH_SHORT); 
       toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 150); 
       toast.show(); 

       finish(); 
       System.exit(0); 
      } 

     } 
相關問題