2013-07-16 16 views
10

Witam wszystkich Używam okna dialogowego alertu z tekstem edycji. Więc chcę ustawić rodzaj wpisu jako hasło do tego edytowanego tekstu programowo. Przeszukałem wiele w google i znajdowałem te dwie metody:jak ustawić typ wejściowy jako hasło do edytowania editekstowego programowo

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

input.setTransformationMethod(PasswordTransformationMethod.getInstance()); 

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

Ale to nie działa dla mnie, pokazując text.But chcę przerywana text.what jest problem dont know.So proszę sugerować mi zrobić taht. Dziękuję wszystkim z góry. jest to kod z okna z edycji tekstu:

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


     } 

Więc pomóż mi, co zrobiłem wrong.Thanks @All

+2

próbowałeś http://stackoverflow.com/questions/2586301/set-inputtype-for-an-edittext? – sandrstar

+0

Tak, próbowałem, ale to nie działa dla mnie – rams

+1

Istnieje specjalny komentarz dla Ciebie pod podanym linkiem: Wystarczy dodać do tego, musisz wywołać setTransformationMethod zamiast setInputType. Wywołanie setInputType po setTransformationMethod powoduje, że EditText nie będzie ponownie w trybie hasła. – sandrstar

Odpowiedz

0

Usuń to i spróbuj

InputType.TYPE_CLASS_TEXT

+0

dziękuję za odpowiedź, to również nie działa – rams

3

tylko spróbować tego input.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

-1

Należy zadzwonić pod numer:

input.setTransformationMethod(PasswordTransformationMethod.getInstance()); 

Zgodnie z opisem here.

Ponadto, jak wspomina w przewidzianym komentuje pytanie:

trzeba zadzwonić setTransformationMethod zamiast setInputType. Wywołanie setInputType po setTransformationMethod powoduje, że EditText nie będzie ponownie w trybie hasła.

Tak powinno wyglądać następująco za mojego zrozumienia:

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

próbowałem, to nie działa – rams

+0

Próbowałem również - i działa dobrze (np. na urządzeniach 4.2). – sandrstar

+0

tak, próbowałem również w 4.2 (tablet). I używam fragmentów, czy jest jakiś problem dla fragmentów – rams

11

otrzymujesz ten problem ponieważ używaszalert.setCustomTitle(title);

po

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

Która ponownie ustawiając go na na normalny typ

albo zmienić: alert.setCustomTitle(title);doalert.setTitle("your title here");

lub jeśli chcesz użyćcustomeTitle

niż stosowanie następujących kodu

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

po

alert.setCustomTitle(title); 
+0

to nie działa – rams

+0

tak, jestem w stanie wpisać i widoczny tekst również – rams

+0

@venkataramana zobacz moją zaktualizowaną odpowiedź! –

Powiązane problemy