2014-03-13 37 views
0

我正在研究一個主佈局應用程序,它包含一個LinearLayout頂部的按鈕行,它包含由edittexts組成的表單。我創建了一個Form對象來保存每個表單的edittext中顯示的所有值。單擊每個按鈕以更改表單的數據以顯示每個表單對象中保存的值。我將Form對象保存在一個List中,以便在按下相應的按鈕時可以訪問每個對象。無法獲取變量以edittext形式顯示值

編輯:我意識到我沒有完全解釋自己。當我點擊其中一個按鈕時,我希望EditText值更改爲與該按鈕對應的一組不同的值。但是當我點擊這些按鈕時,以前輸入的值不會顯示出來。我提到了EditTexts表單的一列,但它只是EditTexts的一列。

當我單擊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, I'm having trouble explaining myself today.

+0

請澄清我們的問題,並提供一些視覺效果如果可能的話。 – Si8

回答

1

提供的代碼似乎不符合您的問題。你想要將值放入EditText視圖,然後點擊一個按鈕,該值將來自EditText視圖的值並將其放置在「表單」中?如果是這樣,那裏面的onCreate()和後請將的setContentView()嘗試:

EditText name = (EditText) findViewById(R.id."edit_text_id"); // where "edit_text_id" is your your EditText view id set in your xml 
String text = name.getText().toSting(); 

TextView textView = (TextView) findViewById(R.id."forms_text_field"); // where "forms_text_field" is your TextView id set in your form's xml 
textView.setText(text); 

重複此爲您的每個EditText上的觀點(在形成相應的TextView設置爲所需的EditText字段值)。這應該工作得很好。

+0

請更正變量名'namet' – geekoraul

+0

@geekoraul很好找。固定:) – zgc7009

+0

感謝您的回覆,但值必須是可編輯的。這就是爲什麼我沒有使用TextView來顯示它們。當我點擊其中一個按鈕時,我想顯示一組與之相對應的輸入EditText值,以便可以根據需要更改它們。 – domi91c