Colore sfondo e edittext

di il
15 risposte

Colore sfondo e edittext

Domanda sciocca!! è possibile cambiare colore di sfondo ad una activity senza cambiare tema???
ed il colore di una edit text??
sempre nella edit text è possibile inserire il . ad una cifra?? es. 1.000 anziché 1000
Grazie

15 Risposte

  • Re: Colore sfondo e edittext

    evans ha scritto:


    Domanda sciocca!! è possibile cambiare colore di sfondo ad una activity senza cambiare tema???
    Certo! Puoi farlo direttamente da xml settando il parametro android:background con il valore che preferisci. Esistono diverse notazioni per i colori (ad esempio #aarrggbb)

    evans ha scritto:


    ed il colore di una edit text??
    Idem come prima. Oppure via codice tramite il metodo "setBackgroundColor(Color.RED)"

    evans ha scritto:


    sempre nella edit text è possibile inserire il . ad una cifra?? es. 1.000 anziché 1000
    Grazie
    Si può fare con un TextWatcher e un NumberFormat!
  • Re: Colore sfondo e edittext

    Grazie, stò utilizzando TextWatcher ma non riesco ad inserire il separatore delle migliaia...non riesco a capire!
  • Re: Colore sfondo e edittext

    Prova con
    number = (EditText) findViewById(R.id.number);
    		
    		TextWatcher watcher = 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) {
    				number.removeTextChangedListener(this);
    				
    				DecimalFormat format = new DecimalFormat();
    				DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    				symbols.setGroupingSeparator('.');
    				format.setDecimalFormatSymbols(symbols);
    				
    				double n = Double.parseDouble(s.toString());
    				number.setText(format.format(n));
    				number.setSelection(number.getText().length());
    				number.addTextChangedListener(this);
    			}
    		};
    		
    		number.addTextChangedListener(watcher);
    pensavo fosse più semplice comunque
  • Re: Colore sfondo e edittext

    No non ci riesco...ma non esiste un modo per farlo direttamente dalle proprietà dell'oggetto?
  • Re: Colore sfondo e edittext

    Prova così:
    
    TextWatcher watcher = 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 (s.toString().trim().length() == 0){
    					return;
    				}
    				number.removeTextChangedListener(this);
    				
    				DecimalFormat format = new DecimalFormat();
    				format.setGroupingUsed(true);
    //				DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    //				symbols.setGroupingSeparator('.');
    //				format.setDecimalFormatSymbols(symbols);
    				
    				String sn = s.toString().replace(",","");
    				double n = Double.parseDouble(sn);
    				Log.d("TAG", "Numero = "+n);
    				number.setText(format.format(n));
    				number.setSelection(number.getText().length());
    				number.addTextChangedListener(this);
    			}
    		};
    
    Se come separatore usa la virgola invece del punto togli il commento a quelle 3 righe
  • Re: Colore sfondo e edittext

    Ti posto il codice di prova:
    package com.example.prova.text;
    
    import java.text.DecimalFormat;
    import java.text.DecimalFormatSymbols;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.util.Log;
    import android.view.Menu;
    import android.widget.EditText;
    
    public class MainActivity extends Activity {
    public EditText number;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		
    		TextWatcher watcher = 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 (s.toString().trim().length() == 0){
    	               return;
    	            }
    	            number.removeTextChangedListener(this);
    	            
    	            DecimalFormat format = new DecimalFormat();
    	            format.setGroupingUsed(true);
    	            DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    	            symbols.setGroupingSeparator('.');
    	            format.setDecimalFormatSymbols(symbols);
    	            
    	            String sn = s.toString().replace(",","");
    	            double n = Double.parseDouble(sn);
    	            Log.d("TAG", "Numero = "+n);
    	            number.setText(format.format(n));
    	            number.setSelection(number.getText().length());
    	            number.addTextChangedListener(this);
    	         }
    
    			@Override
    			public void afterTextChanged(Editable s) {
    				// TODO Auto-generated method stub
    				
    			}
    	      };
    
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    
    }
    
  • Re: Colore sfondo e edittext

    Ciao, ecco gli errori:
    • Compare 2 volte il metodo afterTextChanged
    • non associ la EditText alla tua variabile
    • non associ il TextWatcher alla tua EditText
    Ecco qua il codice (cambia sia il package che l'id della editText)
    
    package com.example.numbers;
    
    import java.text.DecimalFormat;
    import java.text.DecimalFormatSymbols;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.view.Menu;
    import android.widget.EditText;
    
    public class MainActivity extends Activity {
    	public EditText number;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		number = (EditText) findViewById(R.id.number); // Qua va l'id della tua
    														// EditText
    		TextWatcher watcher = 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 (s.toString().trim().length() == 0) {
    					return;
    				}
    				number.removeTextChangedListener(this);
    
    				DecimalFormat format = new DecimalFormat();
    				format.setGroupingUsed(true);
    				DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    				symbols.setGroupingSeparator('.');
    				format.setDecimalFormatSymbols(symbols);
    
    				String sn = s.toString().replace(format.getDecimalFormatSymbols().getGroupingSeparator()+"", "");
    				double n = Double.parseDouble(sn);
    				number.setText(format.format(n));
    				number.setSelection(number.getText().length());
    				number.addTextChangedListener(this);
    			}
    
    		};
    
    		number.addTextChangedListener(watcher);
    
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    
    }
    
  • Re: Colore sfondo e edittext

    Che testa mi ero dimenticatoooo!!!
    Grazie ora è OK
  • Re: Colore sfondo e edittext

  • Re: Colore sfondo e edittext

    Senti se ho 2 edit text devo dichiarare 2 'number'?
    te lo chiedo prima di fare prove inutili....
  • Re: Colore sfondo e edittext

    Beh no, non è possibile.. "number" è il nome della variabile, puoi chiamarla come ti pare...inoltre 2 variabili con lo stesso nome non sono ammesse se hanno la stessa visibilità (es: se entrambe sono 2 variabili di istanza).
    Se anche questa seconda EditText ha bisogno dello stesso "trattamento" della precedente (quindi deve mostrare il separatore delle migliaia) puoi riutilizzare lo stesso TextWatcher
  • Re: Colore sfondo e edittext

    Si forse mi sono espresso male...volevo chiederti se bisogna utilizzare lo stesso TextWatcher per tutti gli edittext presenti.
    ok provo così!
  • Re: Colore sfondo e edittext

    Beh se vuoi puoi crearne uno nuovo, ma se ha gli stessi compiti dell'altro è uno spreco di memoria. Senza contare che poi dovresti fare un "copia e incolla" della sua implementazione visto che è stato creato come classe anonima! Quindi ti conviene usare lo stesso TextWatcher se hai in mente di fornire la stessa rappresentazione delle migliaia!
  • Re: Colore sfondo e edittext

    Scusa la mia ignoranza ma non riesco ad inserire anche la seconda edittext!!
    public class MainActivity extends Activity {
       public EditText number, number1;
       
    
       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
    
          number = (EditText) findViewById(R.id.prova_text); // Qua va l'id della tua   // EditText
          number1 = (EditText) findViewById(R.id.prova_text1);
          
          TextWatcher watcher = 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 (s.toString().trim().length() == 0) {
                   return;
                }
                number.removeTextChangedListener(this);
                number1.removeTextChangedListener(this);
    
                DecimalFormat format = new DecimalFormat();
                format.setGroupingUsed(true);
                DecimalFormatSymbols symbols = new DecimalFormatSymbols();
                symbols.setGroupingSeparator('.');
                format.setDecimalFormatSymbols(symbols);
    
                String sn = s.toString().replace(format.getDecimalFormatSymbols().getGroupingSeparator()+"", "");
                double n = Double.parseDouble(sn);
                number.setText(format.format(n));
                number.setSelection(number.getText().length());
                number.addTextChangedListener(this);
           
             }
    
          };
    
          number.addTextChangedListener(watcher);
          number1.addTextChangedListener(watcher);
    
       }
    
       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
       }
    
    }
    
     
    
Devi accedere o registrarti per scrivere nel forum
15 risposte