0

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

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

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

这是我尝试过的方法,设置了 SharePreferences 以将用户输入存储在 TextWatcher 内的 OnClickListener 内,但是使用此代码,即使 TextWatcher 为空,Plus 按钮的 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 非常陌生,如果需要任何其他信息,请告诉我,非常感谢。

4

1 回答 1

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++;
           }
     });

当需要计算总数时,在 a 中ButtonOnClickListener只需执行以下操作:

@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增量,然后再添加InputWatcher添加行的位置。

于 2012-06-30T10:51:35.170 回答