将按钮上的EditText值设置为自定义对象的值

时间:2014-03-15 22:50:43

标签: android

我正在开发一个应用程序,其主要布局包含一行按钮,该按钮位于一个包含EditTexts列的LinearLayout顶部。我创建了一个Form对象来保存要在Editexts中显示的所有值,并且行中的每个按钮都有一个Form对象。单击每个按钮可更改表单的数据,以显示每个Form对象中保存的值。我将Form对象保存在名为formList的List中,以便在按下相应的按钮时可以访问每个对象。

当我单击其中一个按钮时,我希望EditText值更改为与该按钮对应的另一组值。但是当我单击这些按钮时,先前输入的值不会显示。

当我单击Next Drink时,它会动态创建一个按钮以及一个Form对象,并清除EditTexts,使其看起来像是为新按钮创建了一个新表单。然后我应该能够在EditTexts中添加字符串,并且字符串应该存储在Form对象中,这样如果我再次单击该按钮,Form对象将使用存储在该对象中的字符串填充EditTexts。

因此,如果在单击按钮2后单击按钮3,则应将每个EditText的文本设置为第三个Form对象中包含的值,其中包含Name,Volume,Percentage,Price和Quantity的字符串。

我的问题是单击按钮后,EditTexts中没有显示值。因此,如果单击按钮3,则表单3中的值不会显示。我已经在这里待了很长时间,无法弄清楚为什么,所以我真的很感激任何帮助。这是活动:

package com.example.DrinkApp;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import java.util.ArrayList;
import java.util.List;

public class MyActivity extends Activity {
/**
 * Called when the activity is first created.
 */




//view variables
final Button newDrink[] = new Button[100];
LinearLayout drinkSelectionContainer;
EditText name;
EditText vol;
EditText perc;
EditText pri;
AutoCompleteTextView quant;

//counters
int numOfDrinks = 0;
int selectedForm = 0;


//array to hold all Form objects.
List<Form> formList = new ArrayList<Form>();



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    //get views
    drinkSelectionContainer = (LinearLayout) findViewById(R.id.drinkSelectContainer);
    name = (EditText) findViewById(R.id.etName);
    vol = (EditText) findViewById(R.id.etVol);
    perc = (EditText) findViewById(R.id.etPerc);
    pri = (EditText) findViewById(R.id.etPrice);
    quant = (AutoCompleteTextView) findViewById(R.id.etQuantity);
    Button saveDrink = (Button) findViewById(R.id.bSaveForm);

    Button nextDrink = (Button) findViewById(R.id.bNextDrink);
    Button crunkOut = (Button) findViewById(R.id.bCalc);

    ///////////////////////// next drink method
    addNewDrinkButton();



    name.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            formAdd();

        }

        @Override
        public void afterTextChanged(Editable editable) {

//                mNames.add(selectedForm, name.getText().toString());
//                newDrink[selectedForm].setText(name.getText().toString());

        }
    });
    nextDrink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNewDrinkButton();
        }

    });

}






//listener for drink select buttons. when button is clicked, it finds out which one it was,
//and then displays the correct values.
public View.OnClickListener drinkSelectListener = new View.OnClickListener() {
    public void onClick(View v) {

        selectedForm = v.getId();
        Form form = formList.get(selectedForm);
        Button v1 = (Button) findViewById(selectedForm);
        v1.setText(" " + selectedForm);
        // Do something depending on the value of the tag
        Log.v("My Logs:", "Select Drink " + selectedForm + " button pressed.");
        Log.v("My Logs:", "Drink " + selectedForm + " name: " + form.getName());


        formDisplay(form);


    }
};


private void addNewDrinkButton() {
    formClear();
    formAdd();
    numOfDrinks++;

    selectedForm = numOfDrinks;
    Log.v("My Logs:", "Next Drink button pressed.");


    newDrink[numOfDrinks] = new Button(getBaseContext());
    newDrink[numOfDrinks].setOnClickListener(drinkSelectListener);
    newDrink[numOfDrinks].setId(numOfDrinks);


    newDrink[numOfDrinks].setText("" + selectedForm);
    newDrink[numOfDrinks].setLayoutParams(new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    // name.setText("Drink " + (numOfDrinks+1));

    drinkSelectionContainer.addView(newDrink[numOfDrinks]);



    //TODO: save form values, clear edit texts, and create new variables for edittexts.    
}


void formAdd(){

    Form tempForm = new Form();

    tempForm.setId(selectedForm);
    tempForm.setName(name.getText().toString());
    tempForm.setVolume(vol.getText().toString());
    tempForm.setPercentage(perc.getText().toString());
    tempForm.setPrice(pri.getText().toString());
    tempForm.setQuantity(quant.getText().toString());

    formList.add(selectedForm, tempForm);

}

void formDisplay(Form form){

    name.setText(form.getName());
    vol.setText(form.getVolume());
    perc.setText(form.getPercentage());
    pri.setText(form.getPrice());
    quant.setText(form.getQuantity());


}


void formClear(){

    name.setText("");
    vol.setText("");
    perc.setText("");
    pri.setText("");
    quant.setText("");


    }
} 

Here's the main layout. So if I click button 4, I want to have the EditTexts display the values I'd entered the last time I'd clicked on it. It's really very simple.

2 个答案:

答案 0 :(得分:0)

您在formAdd()之前调用formClear()?

答案 1 :(得分:0)

按下按钮

设置EditText值
  

edittext.setText("nexteditText"); edittext.requestfocus();