-3

I am developing an application for Android.

My application contains ten buttons, to which I have set an onclicklistener() method. The ten buttons contains the digits 0-9.

Now, if I click any two or three buttons among the ten buttons, the corresponding digits must be entered into edit text and it must be shown in the edittext box.

I am able to display the single digit if I click on any of the buttons, but if I click on another button, then the previous value disappears and the new value is shown.

But what I want is this: no matter how many buttons I click, that no. of digits will appear in the edittext box.

Please can anyone explain to me the code, or give me a hint so that it can be made in a simpler way.

Kieran
  • 2,554
  • 3
  • 26
  • 38

6 Answers6

1

You are using editText.setText("");

Instead you must use editText.append();

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Dany's
  • 943
  • 1
  • 7
  • 17
1

Using Shared Preferences:

I think, you may used Shared Preferences when you button was click, get value from Edittext and put on shared preferences. After click next button get that shared preferences value. You may used each button click put on value shared preferences.

Go to this problem, which is help you to solve: >> SharedPreference problem in android

Using Intent:

May be used this code on button click event:


            Bundle extras = getIntent().getExtras();
            String value1 = extras.getString("Value1");
    String value2 = extras.getString("Value2");
    if (value1 != null && value2 != null) {
        EditText text1 = (EditText) findViewById(R.id.EditText01);
        EditText text2 = (EditText) findViewById(R.id.EditText02);
        text1.setText(value1);
        text2.setText(value2);
    }

Other useful resources: Get Value of a Edit Text field

http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-edittext-controls/

http://www.java2s.com/Code/Android/UI/GetvaluefromEditText.htm

http://geekswithblogs.net/bosuch/archive/2011/01/17/android---passing-data-between-activities.aspx

Community
  • 1
  • 1
Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81
0

In all 0-9 buttons onclick event you can write following code.

editText.setText((editText.getText().toString) +""+ nevText);

When you click on button then above code set newText with previous text in edittext box.

Ranjitsingh Chandel
  • 1,479
  • 6
  • 19
  • 33
0

You can take a public static String variable and concat the new value to previous and set it to EditText

deepak Sharma
  • 1,641
  • 10
  • 23
0

Use below code

    public class AsActivity extends Activity {
/** Called when the activity is first created. */
Button b1,b2,b3;
EditText et;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b1=(Button)findViewById(R.id.button1);
    b2=(Button)findViewById(R.id.button2);
    b3=(Button)findViewById(R.id.button3);
    et=(EditText)findViewById(R.id.editText1);
}
public void onclick(View v){
    switch(v.getId()){
    case R.id.button1:
        et.append("1");
        break;
    case R.id.button2:
        et.append("2");
        break;
    case R.id.button3:
        et.append("3");
        break;
    }
}
}

here i have use property of button you can use switch statement as shown above in ur onclicklistener

Khan
  • 7,585
  • 3
  • 27
  • 44
-3

You should use the EditText append() method which appends data to the EditText.

So each time a new button is clicked just use :

 myEdtiText.append(str);
Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84