我遇到了无法从第二个膨胀视图获取用户输入的问题。我已经上传了一张图片到这个网站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 非常陌生,如果需要任何其他信息,请告诉我,非常感谢。