Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

android - automatically update my activity to show which was written

Like you can see here, the user must provide a sim card number and when this number is provided, i would like to fill the content automatically by myself adding the sim card number which was provided. I think that i have to update automatically (Autoupdate?) the activity or something like that. If anyone can help me by editing what i have done or by presenting a sample example, it will be very helpfull! Thank you very much

The image of my application is here: http://imageshack.us/a/img199/1561/12216127.png

My code is here:

    public class RegulationTime extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {

       EditText edit2;

       super.onCreate(savedInstanceState);
       setContentView(R.layout.regulation_time);
       // Show the Up button in the action bar.
       setupActionBar();

       //Récupérer le n° de la carte SIM saisi par l'utilisateur
       EditText edit1 = (EditText) findViewById(R.id.regedit1);
       String simCard = edit1.getText().toString();

       //Récupérer le n° du destinataire & le saisir automatiquement dans l'EditText
       String recoverRN = MasterNumber.getDefaults("mehmet", RegulationTime.this);
       edit2 = (EditText) findViewById(R.id.regedit2);
       edit2.setText(recoverRN);

       //On rentre automatiquement le texte "#152#SIMCardNumber#" à la place de l'utilisateur
       EditText edit3 = (EditText) findViewById(R.id.regedit3);
       String concatenation;
       if(simCard.length()>0)
       {
           concatenation = "#152#" + simCard + "#";
           edit3.setText(concatenation);
       }
   }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What you want is TextWatcher. Use onTextChanged() so that as the user types in the "Sim card" box, the "content" box is filling up. So attach the TextWatcher to your first EditText

edit1.addTextChangedListener(new TempWatcher());

and create an inner class for TextWatcher

private class TempWatcher implements TextWatcher {

    @Override
    public void afterTextChanged(Editable s) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) 
    {   
         if (s.length() > 0) 
         {
             String sim = s.toString();
             edit2.setText(sim);
         }

    }

You will probably want to make your EditTexts member variables so declare them before onCreate() then initialize them as you are in onCreate(). This should get you started. I wrote it quickly so you may need to tweak variable names, error checking if needed, and such. Let me know if that helps

Edit

public class RegulationTime extends Activity {

  EditText edit1, edit2;   // declare them here so they are member variables

 @Override
   protected void onCreate(Bundle savedInstanceState) {


   super.onCreate(savedInstanceState);
   setContentView(R.layout.regulation_time);
   // Show the Up button in the action bar.
   setupActionBar();

   //Récupérer le n° de la carte SIM saisi par l'utilisateur
   edit1 = (EditText) findViewById(R.id.regedit1);   // initialize here
   String simCard = edit1.getText().toString();

   //Récupérer le n° du destinataire & le saisir automatiquement dans l'EditText
   String recoverRN = MasterNumber.getDefaults("mehmet", RegulationTime.this);
   edit2 = (EditText) findViewById(R.id.regedit2);   // initialize here
   edit2.setText(recoverRN);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...