Question

For starters, any help would be greatly appreciated! I have created a numerical keypad and I need to pass the numeric values to an EditText every time one of the buttons is selected. I'm running into a problem where the EditText is being overwritten by setText, every time a different button is pressed. I really need to concatenate each value, and I'm not quite sure how to do this. Any of the one to nine buttons could be pressed in any order.

Here is some of the code. I'm just trying to get these keys working first.

    View hash = findViewById(R.id.keypad_hash);
    hash.setOnClickListener(this);
    View key1 = findViewById(R.id.keypad_1);
    key1.setOnClickListener(this);
    View key2 = findViewById(R.id.keypad_2);
    key2.setOnClickListener(this);

}




@Override
public void onClick(View v){
switch(v.getId()){
case R.id.keypad_hash:
    questions();
    break;

case R.id.keypad_1:

    final EditText number_edit_text1 = (EditText) this.findViewById(R.id.Edit);
      number_edit_text1.setText(String.valueOf("1"));




      break;


case R.id.keypad_2:

    final EditText number_edit_text2 = (EditText) this.findViewById(R.id.Edit);
      number_edit_text2.setText(String.valueOf("2"));
    break;

}   
}

and then the edittext in the layout

<EditText  
android:id="@+id/Edit"  
android:layout_height="wrap_content"    
android:inputType="number"  
android:layout_width="fill_parent"
android:numeric="integer">  
</EditText> 
Was it helpful?

Solution

Try:

number_edit_text2.append(String.valueOf("2"));

If that for some reason doesn't work:

number_edit_text2.setText(number_edit_text2.getText().toString()+String.valueOf("2"));

Also, on a side note, you can make the process easier on yourself.

In your xml, you should do:

android:tag="0"

and replace 0 with whatever number you want for each button.

In your class body, you should declare EditText editText;, then in onCreate, you should do editText = (EditText)findViewById(R.id.Edit);

Then in onClick, just do:

editText.append(String.valueOf(v.getTag()));

This should simplify your code, make it more manageable, and use ever so slightly fewer resources since you don't have to repeatedly recreate the EditText.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top