2013-07-16 8 views
10

Ciao a tutti sto usando la finestra di avviso con testo di modifica. Voglio impostare il tipo di input come password per quel testo di modifica in modo programmatico. Ho cercato molto in google e scoprire questi due metodi:come impostare il tipo di input come password per edittext in modo programmatico

final EditText input = new EditText(getActivity()); 

input.setTransformationMethod(PasswordTransformationMethod.getInstance()); 

input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 

Ma non sta lavorando per me, che mostra il text.But i vogliono text.what tratteggiata è il problema io non know.So per favore mi suggeriscono di fare taht. Grazie a tutti in anticipo. questo è il codice della finestra di dialogo con modificare il testo:

public void showDialog(){ 

      /* Alert Dialog Code Start*/  
       AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); 
//    alert.setTitle("JPOP"); //Set Alert dialog title here 
       alert.setMessage("    Please enter password"); //Message here 

       Log.e("dialog in password ","passworddddddddddddddddd"); 

       // Set an EditText view to get user input 
       final EditText input = new EditText(getActivity()); 
//    input.setInputType(InputType.TYPE_CLASS_TEXT); 
//    input.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); 
//    input.setTransformationMethod(PasswordTransformationMethod.getInstance()); 

//    final EditText input = new EditText(getActivity()); 
       input.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); 
       input.setTransformationMethod(new PasswordTransformationMethod()); 


       input.setHint("Password"); 
       input.setSingleLine(); 
       input.setTextSize(14); 
       alert.setView(input); 

       alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 

        strPassword = input.getEditableText().toString().trim();  

        if(strPassword.length()!=0){ 

       String prestatus =DataUrls.preferences.getString("Password", ""); 
       if(prestatus.equals(strPassword)){ 

        if(price_reports_check){ 

         price_reports_check=false; 

          ReportsFragment reportfragment = new ReportsFragment(); 
          FragmentManager fragmentManager = getFragmentManager(); 
          FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
          fragmentTransaction.replace(R.id.details, reportfragment); 
          fragmentTransaction.commit(); 
        }else{ 
         PriceListFragment pricelistfragment = new PriceListFragment(); 
         FragmentManager fragmentManager = getFragmentManager(); 
         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
         fragmentTransaction.replace(R.id.details, pricelistfragment); 
         fragmentTransaction.commit(); 
        } 

       }else 
       { 
        showDialog(); 
        Toast.makeText(getActivity(), "The password you entered is wrong", Toast.LENGTH_SHORT).show(); 
       } 

        } 
        else 
        { 
         showDialog(); 
         Toast.makeText(getActivity(), "Please Enter Password", Toast.LENGTH_SHORT).show(); 

        } 

       } // End of onClick(DialogInterface dialog, int whichButton) 
      }); //End of ok.... 

       alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
        // Canceled. 
         dialog.cancel(); 
        } 
      }); //End of alert.setNegativeButton 


       AlertDialog alertDialog = alert.create(); 

        TextView title = new TextView(getActivity()); 
        // You Can Customise your Title here 
        title.setText("JPOP"); 
//     title.setBackgroundColor(Color.DKGRAY); 
        title.setPadding(10, 10, 10, 10); 
        title.setGravity(Gravity.CENTER); 
//     title.setTextColor(Color.WHITE); 
        title.setTextSize(20); 
        alert.setCustomTitle(title); 
        alert.setCancelable(false); 

        alert.show(); 


     } 

Quindi, per favore mi aiuti quello che ho fatto wrong.Thanks @All

+2

Hai provato http://stackoverflow.com/questions/2586301/set-inputtype-for-an-edittext? – sandrstar

+0

sì, ho provato ma non funziona per me – rams

+1

C'è un commento speciale per te nel link fornito: Solo per aggiungere a questo, è necessario chiamare setTransformationMethod invece di setInputType. La chiamata a setInputType dopo setTransformationMethod fa sì che EditText non si trovi nuovamente in modalità password. – sandrstar

risposta

0

Rimuovere questo e cercare

InputType.TYPE_CLASS_TEXT

+0

grazie per la tua risposta, anche questo non funziona – rams

3

solo provare questo input.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

-1

È necessario chiamare:

input.setTransformationMethod(PasswordTransformationMethod.getInstance()); 

Come descritto here.

Inoltre, come commenta cita nel previsto domanda:

è necessario chiamare setTransformationMethod invece di setInputType. La chiamata a setInputType dopo setTransformationMethod fa sì che EditText non si trovi nuovamente in modalità password.

Quindi, dovrebbe essere simile al seguente per la mia comprensione:

input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 
    input.setTransformationMethod(PasswordTransformationMethod.getInstance()); 
+0

ho provato, non funziona – rams

+0

l'ho provato anche io - e funziona bene (ad esempio su 4.2 dispositivi). – sandrstar

+0

sì, ho anche provato in 4.2 (tablet). E sto usando i frammenti, c'è qualche problema per i frammenti – rams

11

hai trovato questo problema perché si sta utilizzandoalert.setCustomTitle(title);

dopo

input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 
     input.setTransformationMethod(PasswordTransformationMethod.getInstance()); 

Quale rendendolo di nuovo al normale tipo

sia cambiare: alert.setCustomTitle(title);peralert.setTitle("your title here");

o se si desidera utilizzare customeTitle

che utilizzare seguente codice

input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 
input.setTransformationMethod(PasswordTransformationMethod.getInstance()); 
alert.setView(input); 

dopo

alert.setCustomTitle(title); 
+0

non funziona – rams

+0

sì, sono in grado di digitare e testo visibile anche – rams

+0

@venkataramana vedere la mia risposta aggiornata! –

Problemi correlati