-1

I have a problem As it can be seen in the picture there are 3 Buttons and a EditText

enter image description here

Need to write in the box which button is pressed and write the corresponding character in the EditText. Just like a keyboard. Sorry for my bad English. Like this:

enter image description here

Thanks

4

2 回答 2

1

You should define a StringBuilder, then every time you press a button add that characted to the StringBuilder and update the content of EditText.

Just a quick snippet:

    StringBuilder s = new StringBuilder();
    EditText et = (EditText) findViewById(EDITTEXT_ID_PATH);

    Button button_q = (Button) findViewById(BUTTON_Q_ID_PATH);
    button_q.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                s.append("q");
                et.setText(s);
            }
        });

    Button button_e = (Button) findViewById(BUTTON_E_ID_PATH);
    button_e.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                s.append("e");
                et.setText(s);
            }
        });

    Button button_w = (Button) findViewById(BUTTON_W_ID_PATH);
    button_q.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                s.append("w");
                et.setText(s);
            }
        });
于 2013-03-30T15:57:33.193 回答
0

You can also use switch case, here is the completed code

public class MainActivity extends Activity implements OnClickListener{
    EditText et1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b1 = (Button)findViewById(R.id.button1);
        Button b2 = (Button)findViewById(R.id.button2);
        Button b3 = (Button)findViewById(R.id.button3);
        et1 = (EditText)findViewById(R.id.editText1);

        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.button1:
                et1.append("Q");
                break;
            case R.id.button2:
                et1.append("E");
                break;
            case R.id.button3:
                et1.append("W");
                break;
            default:
                break;
            }
    }

}
于 2013-03-30T16:04:01.920 回答