如何从多个按钮获取值并将其设置在多个edittext -Android上

时间:2014-02-20 08:24:54

标签: java android android-layout

我有4个Edittext和11个按钮。我想使用这11个按钮在edittext上显示不同的文本。 我确实尝试过carat.setText(carat.getText()。toString()+((Button)v).getText());但它在所有4个edittext上都有相同的值。 我有4个名为克拉,价格,总数,列表的编辑文本,我想使用这些按钮将值插入其中。

如果我使用list.setText(list.getText()。toString()+((Button)v).getText()); total.setText(total.getText()。toString()+((Button)v).getText());在最后一行,然后当我使用按钮输入值时,相同的值同时输入所有的edittext。但我不希望这样。我想分别在每个edittext中输入值。

EditText carat,list,price,total;
Button submit;
Button one, two, three, four, five, six, seven, eight, nine, zero, cancel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calculator);

    carat=(EditText)findViewById(R.id.carat);
    list=(EditText)findViewById(R.id.list);
    price=(EditText)findViewById(R.id.price);
    total=(EditText)findViewById(R.id.total);
    submit=(Button)findViewById(R.id.submit);

    one = (Button) findViewById(R.id.one);
    two = (Button) findViewById(R.id.two);
    three = (Button) findViewById(R.id.three);
    four = (Button) findViewById(R.id.four);
    five = (Button) findViewById(R.id.five);
    six = (Button) findViewById(R.id.six);
    seven = (Button) findViewById(R.id.seven);
    eight = (Button) findViewById(R.id.eight);
    nine = (Button) findViewById(R.id.nine);
    zero = (Button) findViewById(R.id.zero);
    cancel=(Button)findViewById(R.id.clear);

    Spinner shape2=(Spinner)findViewById(R.id.shape2);
    ArrayAdapter<CharSequence> adapter11 = ArrayAdapter.createFromResource(this, R.array.shape2, android.R.layout.simple_spinner_item);
    adapter11.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    shape2.setAdapter(adapter11);

    Spinner cut3=(Spinner)findViewById(R.id.cut3);
    ArrayAdapter<CharSequence> adapter12 = ArrayAdapter.createFromResource(this, R.array.cut3, android.R.layout.simple_spinner_item);
    adapter12.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    cut3.setAdapter(adapter12);

    Spinner color=(Spinner)findViewById(R.id.color);
    ArrayAdapter<CharSequence> adapter13 = ArrayAdapter.createFromResource(this, R.array.color, android.R.layout.simple_spinner_item);
    adapter13.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    color.setAdapter(adapter13);

    one.setOnClickListener(this);
    two.setOnClickListener(this);
    three.setOnClickListener(this);
    four.setOnClickListener(this);
    five.setOnClickListener(this);
    six.setOnClickListener(this);
    seven.setOnClickListener(this);
    eight.setOnClickListener(this);
    nine.setOnClickListener(this);
    zero.setOnClickListener(this);
    cancel.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    carat.setText(carat.getText().toString() + ((Button) v).getText());
    list.setText(list.getText().toString() + ((Button) v).getText());
    total.setText(total.getText().toString() + ((Button) v).getText());
}

0 个答案:

没有答案