从动态创建的视图中获取用户输入

时间:2012-06-30 06:57:55

标签: android user-input data-storage android-inflate textwatcher

我遇到的问题是无法从第二个膨胀的视图中获取用户输入。 我已将图片上传到此网站http://postimage.org/image/b4syhdzrr/ 因为我仍然不允许直接在SO上发布图像。

如您所见,TextView仅显示前两个EditText输入+计算,但第三个输入未被考虑。(数字的第一个EditText未膨胀)

因为我不确定最终用户需要多少EditText。 我该如何从所有EditText获取用户输入?

以下是我尝试过的,设置SharePreferences以将用户输入存储在TextWatcher内部的OnClickListener中,但是使用此代码,即使TextWatcher为空,On按钮的OnClickListener也会崩溃应用程序。

public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            int position = spinner.getSelectedItemPosition();
            switch (position) {
            case 0:
                ll = (LinearLayout) findViewById(R.id.llSetView);
                ll.removeAllViews();
                View person1 = View.inflate(BillCalculator1.this,R.layout.person1, null);
                ll.addView(person1);

                btP1Add = (Button) ll.findViewById(R.id.buttonP1Add);
                btP1Gst = (Button) ll.findViewById(R.id.buttonP1GST);
                btP1Add.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        llNewRow = (LinearLayout) findViewById(R.id.llP1AddNewRow);
                        etNewRow = (EditText) findViewById(R.id.editTextNewRow);
                        View newRow = View.inflate(BillCalculator1.this,R.layout.newrow, null);
                        llNewRow.addView(newRow);

                        TextWatcher input = new TextWatcher() {
                            public void afterTextChanged(Editable s) {
                            }
                            //SharedPreferences here

                            public void beforeTextChanged(CharSequence s,
                                    int start, int count, int after) {
                            }

                            public void onTextChanged(CharSequence s,
                                    int start, int before, int count) {
                            }
                        };

                    }
                });

膨胀视图的XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/editTextNewRow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="numberDecimal"
    android:hint="Enter new amount">

    <requestFocus />
</EditText>

对于编程和Android来说很新,请告诉我是否需要任何其他信息,非常感谢。

1 个答案:

答案 0 :(得分:0)

我会尝试另一种方法来解决你想要做的事情。我会向夸大的TextWatcher添加一个特殊的EditText,并将用户输入的值存储在ArrayList中。首先,你需要在课堂上增加两个字段:

private ArrayList<String> mData = new ArrayList<String>(); // this will store the entered values
private static int counter = 0; // to identify the rows

创建一个实现TextWatcher接口的类,如下所示:

    public class InputWatcher implements TextWatcher {

            private int mRowNumber;

            public InputWatcher(int rowNumber) {
                mRowNumber = rowNumber;
            }

            @Override
            public void afterTextChanged(Editable s) {
                // here you'll add the text as the user enters it in the correct position in the
                mData.set(mRowNumber, s.toString());                    
            }
     }

然后:

int position = spinner.getSelectedItemPosition();
switch (position) {
case 0:
     mData.clear();
     counter = 0;
     ll = (LinearLayout) findViewById(R.id.llSetView);
     ll.removeAllViews();
     // I sure hope that you have the Buttons with the id button1Add and buttonP1GST in the layout that you inflate
     // otherwise the code will throw a NullPointerException when you use them below
     View person1 = View.inflate(BillCalculator1.this,R.layout.person1, null);
     ll.addView(person1);
     btP1Add = (Button) ll.findViewById(R.id.buttonP1Add);
     btP1Gst = (Button) ll.findViewById(R.id.buttonP1GST);
     btP1Add.setOnClickListener(new View.OnClickListener() {

           public void onClick(View v) {
                 // add an entry in the data holder for this row
        mData.add("");
        View newRow = View.inflate(BillCalculator1.this,
                        R.layout.newrow, null);
        EditText justAdded = (EditText) newRow
                        .findViewById(R.id.editTextNewRow);
        justAdded.addTextChangedListener(new InputWatcher(counter));
        ll.addView(newRow);
        counter++;
           }
     });

在计算总数时,在Button的{​​{1}}中,只需执行以下操作:

OnClickListener

从您的图片中我不明白您是否已经从布局中的一个@Override public void onClick(View v) { int storedSize = mData.size(); int sum = 0; for (int i = 0; i < storedSize; i++) { sum += Integer.parseInt(mData.get(i).equals("") ? "0" : mData.get(i)); } Toast.makeText(getApplicationContext(), "Total" + sum, Toast.LENGTH_SHORT).show(); } 开始(+ EditText左侧的那个)。如果是这种情况,那么您必须手动设置Button并移动InputWatcher增量,然后再添加行添加counter