2015-08-29 1 views
0

J'essaie d'obtenir le même résultat que celui indiqué dans here. Mais aucune image ne se prépare. De plus, je dois cliquer sur chaque edittext pour taper un nombre. Je veux que le curseur se déplace au prochain edittext quand le précédent est entré (maxLength = 1). Le "star1" est l'image avec l'étoile et "vide" est l'image sans étoile. Ce serait vraiment utile si quelqu'un peut me dire ce que j'ai fait de mal.impossible de définir une image personnalisée dans edittext

activity_main 
 

 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:layout_width="fill_parent" 
 
    android:layout_height="fill_parent" 
 
    android:orientation="vertical" > 
 
    
 
     <TableLayout 
 
      android:layout_width="fill_parent" 
 
      android:layout_height="wrap_content" 
 
      android:orientation="vertical" 
 
      android:layout_marginTop="40dp" > 
 

 
       <TableRow 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:gravity="center_horizontal" > 
 

 
        
 

 
        <EditText 
 
         android:id="@+id/ET1" 
 
         android:layout_height="70dp" 
 
         android:layout_width="70dp" 
 
         android:singleLine="true" 
 
         android:maxLength="1" 
 
         android:inputType="number" 
 
         android:background="@drawable/round" > 
 
        </EditText> 
 

 
        
 

 
        <EditText 
 
         android:id="@+id/ET2" 
 
         android:layout_height="70dp" 
 
         android:layout_width="70dp" 
 
         android:singleLine="true" 
 
         android:maxLength="1" 
 
         android:inputType="number" 
 
         android:background="@drawable/round" > 
 
        </EditText> 
 
        
 
        <EditText 
 
         android:id="@+id/ET3" 
 
         android:layout_height="70dp" 
 
         android:layout_width="70dp" 
 
         android:singleLine="true" 
 
         android:maxLength="1" 
 
         android:inputType="number" 
 
         android:background="@drawable/round" > 
 
        </EditText> 
 
        
 
        <EditText 
 
         android:id="@+id/ET4" 
 
         android:layout_height="70dp" 
 
         android:layout_width="70dp" 
 
         android:singleLine="true" 
 
         android:maxLength="1" 
 
         android:inputType="number" 
 
         android:background="@drawable/round" > 
 
        </EditText> 
 

 
      </TableRow> 
 

 
     </TableLayout> 
 

 
     
 
    
 
    
 
    
 

 
</LinearLayout>
Main Activity 
 

 
public class MainActivity extends Activity { 
 
\t 
 
\t EditText ET1, ET2, ET3, ET4; 
 

 
\t @Override 
 
\t protected void onCreate(Bundle savedInstanceState) { 
 
\t \t super.onCreate(savedInstanceState); 
 
\t \t setContentView(R.layout.activity_main); 
 
\t \t 
 
\t \t ET1 = (EditText)findViewById(R.id.ET1); 
 
\t \t ET2 = (EditText)findViewById(R.id.ET2); 
 
\t \t ET3 = (EditText)findViewById(R.id.ET3); 
 
\t \t ET4 = (EditText)findViewById(R.id.ET4); 
 
\t } // onCreate ends 
 
\t 
 
\t 
 
\t private void handleEdittextListner(){ 
 
     try { 
 
      ET1.addTextChangedListener(new TextWatcher() { 
 
    
 
       @Override 
 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 
 
        // TODO Auto-generated method stub 
 
    
 
       } 
 
    
 
       @Override 
 
       public void beforeTextChanged(CharSequence s, int start, int count, 
 
         int after) { 
 
        // TODO Auto-generated method stub 
 
    
 
       } 
 
    
 
       
 

 
\t \t \t \t @Override 
 
\t \t \t \t public void afterTextChanged(Editable s) { 
 
\t \t \t \t \t // TODO Auto-generated method stub 
 
\t \t \t \t \t if(ET1.getText().toString().trim().length()==1){ 
 
\t \t \t \t \t \t 
 
         ET1.clearFocus(); 
 
         ET2.requestFocus(); 
 
         ET1.setBackgroundResource(R.drawable.star1); 
 
        } 
 
\t \t \t \t } 
 
      }); 
 
    
 
      ET2.addTextChangedListener(new TextWatcher() { 
 
    
 
       @Override 
 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 
 
        // TODO Auto-generated method stub 
 
    
 
       } 
 
    
 
       @Override 
 
       public void beforeTextChanged(CharSequence s, int start, int count, 
 
         int after) { 
 
        // TODO Auto-generated method stub 
 
    
 
       } 
 
    
 
       @Override 
 
       public void afterTextChanged(Editable s) { 
 
        if(ET2.getText().toString().trim().length()==1){ 
 
    
 
         ET2.clearFocus(); 
 
         ET3.requestFocus(); 
 
         ET2.setBackgroundResource(R.drawable.star1); 
 
        } 
 
       } 
 
      }); 
 
      ET3.addTextChangedListener(new TextWatcher() { 
 
       @Override 
 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 
 
       } 
 
       @Override 
 
       public void beforeTextChanged(CharSequence s, int start, int count, 
 
         int after) { 
 
       } 
 
       @Override 
 
       public void afterTextChanged(Editable s) { 
 
        if(ET3.getText().toString().trim().length()==1){ 
 
    
 
         //ET1.clearFocus(); 
 
         ET3.clearFocus(); 
 
         ET4.requestFocus(); 
 
    
 
         ET3.setBackgroundResource(R.drawable.star1); 
 
        } 
 
    
 
       } 
 
      }); 
 
      
 
      ET4.addTextChangedListener(new TextWatcher() { 
 
       @Override 
 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 
 
       } 
 
       @Override 
 
       public void beforeTextChanged(CharSequence s, int start, int count, 
 
         int after) { 
 
       } 
 
       @Override 
 
       public void afterTextChanged(Editable s) { 
 
        if(ET4.getText().toString().trim().length()==1){ 
 
         ET4.setBackgroundResource(R.drawable.star1); 
 
         //Hide keyboard 
 
         InputMethodManager imm = 
 
           (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
 
         imm.hideSoftInputFromWindow(ET4.getWindowToken(), 0); 
 
        } 
 
    
 
       } 
 
      }); 
 
      this.ET2.setOnKeyListener(new View.OnKeyListener() 
 
      { 
 

 
\t \t \t \t @Override 
 
\t \t \t \t public boolean onKey(View paramView, int paramInt, KeyEvent paramKeyEvent) { 
 
\t \t \t \t \t // TODO Auto-generated method stub 
 
\t \t \t \t \t if ((paramKeyEvent.getAction() == KeyEvent.ACTION_DOWN)&&(paramInt == 67) && (MainActivity.this.ET2.getText().length() == 0)){ 
 
         ET1.requestFocus(); 
 
         ET1.setBackgroundResource(R.drawable.empty); 
 
         ET1.setText(""); 
 
        } 
 
\t \t \t \t \t return false; 
 
\t \t \t \t } 
 
       
 
      }); 
 
      this.ET3.setOnKeyListener(new View.OnKeyListener() 
 
      { 
 
       public boolean onKey(View paramView, int paramInt, KeyEvent paramKeyEvent) 
 
       { 
 
        if ((paramKeyEvent.getAction() == KeyEvent.ACTION_DOWN)&&(paramInt == 67) && (MainActivity.this.ET3.getText().length() == 0)){ 
 
         ET2.requestFocus(); 
 
         ET2.setBackgroundResource(R.drawable.empty); 
 
         ET2.setText(""); 
 
        } 
 
    
 
        return false; 
 
       } 
 
      }); 
 
      this.ET4.setOnKeyListener(new View.OnKeyListener() 
 
      { 
 
       public boolean onKey(View paramView, int paramInt, KeyEvent paramKeyEvent) 
 
       { 
 
        if ((paramKeyEvent.getAction() == KeyEvent.ACTION_DOWN)&&(paramInt == 67) && (MainActivity.this.ET4.getText().length() == 0)){ 
 
         ET3.requestFocus(); 
 
         ET3.setBackgroundResource(R.drawable.empty); 
 
         ET3.setText(""); 
 
        } 
 
    
 
        return false; 
 
       } 
 
      }); 
 
      
 
     } catch (Exception e) { 
 
      e.toString(); 
 
     } 
 
    } 
 

 
\t 
 
} // Activity ends

+0

Vous souhaitez utiliser cette disposition pour la saisie du mot de passe? – zkminusck

+0

Cette disposition ou tout autre. Je veux juste placer l'image faite sur commande au lieu de. (Point) dans tel 4 EditText –

+0

Je suis capable de changer le point en "*" en utilisant PasswordTransformationMethod. Mais je veux mettre l'image au lieu de "*" –

Répondre

0

d'abord dans votre mise en page fichier xml définir une couleur de fond blanc à votre élément de mise en page de la racine comme:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/white" 
    android:orientation="vertical" > 

Après cela, dans tous les EditText de mettre la même couleur pour le texte:

android:textColor="@android:color/white" 

Après faire quelque chose comme:

EditText ET1, ET2, ET3, ET4; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ET1 = (EditText)findViewById(R.id.ET1); 
    ET2 = (EditText)findViewById(R.id.ET2); 
    ET3 = (EditText)findViewById(R.id.ET3); 
    ET4 = (EditText)findViewById(R.id.ET4); 

    ET1.addTextChangedListener(new TextWatcher() { 

     public void afterTextChanged(Editable s) { 
      if(s.length() == 1){ 
       ET1.setBackgroundResource(R.drawable.star1); 
       if(ET2.requestFocus()){ 
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
       } 
      } 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      // TODO Auto-generated method stub 
     } 

     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // TODO Auto-generated method stub 
     } 
    }); 

    ET2.addTextChangedListener(new TextWatcher() { 

     public void afterTextChanged(Editable s) { 
      if(s.length() == 1){ 
       ET2.setBackgroundResource(R.drawable.star1); 
       if(ET3.requestFocus()){ 
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
       } 
      } 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      // TODO Auto-generated method stub 
     } 

     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // TODO Auto-generated method stub 
     } 
    }); 

    ET3.addTextChangedListener(new TextWatcher() { 

     public void afterTextChanged(Editable s) { 
      if(s.length() == 1){ 
       ET3.setBackgroundResource(R.drawable.star1); 
       if(ET4.requestFocus()){ 
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
       } 
      } 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      // TODO Auto-generated method stub 
     } 

     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // TODO Auto-generated method stub 
     } 
    }); 

    ET4.addTextChangedListener(new TextWatcher() { 

     public void afterTextChanged(Editable s) { 
      if(s.length() == 1){ 
       ET3.setBackgroundResource(R.drawable.star1); 
       InputMethodManager imm = 
         (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(ET4.getWindowToken(), 0); 
      } 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      // TODO Auto-generated method stub 
     } 

     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // TODO Auto-generated method stub 
     } 
    }); 
} 

EDIT:

Essayez de mettre votre EditText dans un FrameLayout. Dans chaque FrameLayout ensemble android:background="@drawable/round" comme:

<FrameLayout 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:background="@drawable/round" > 
    <EditText 
     android:id="@+id/ET1" 
     android:layout_height="70dp" 
     android:layout_width="70dp" 
     android:singleLine="true" 
     android:maxLength="1" 
     android:inputType="number"> 
     </EditText> 
    </FrameLayout> 
+0

Merci beaucoup pour le code. Maintenant, les images fonctionnent. Mais la frontière (android: background = "@ drawable/round") de EditText disparaît également. Je veux que l'image soit à l'intérieur de la frontière. Est-ce qu'il y a un moyen pour ça? –

+0

Heureux que ça vous a aidé! Voir ma réponse éditée pour la solution de problème de fond. – zkminusck

+0

Le problème de fond est également résolu grâce à l'homme. –