我在android studio上的项目有问题,我需要你的帮助。我创建了一个包含3个EditText
和一个Textview
的行。我想在EditText
上写下数字并计算Textview
的平均值。我点击它时也使用按钮创建一个新行。我在第一行实现了计算,但每次我尝试计算按钮创建的行时,都没有结果。这是我的代码:
public class MainActivity extends AppCompatActivity {
private LinearLayout parentLinearLayout;
private BreakIterator resultsText;
EditText editText1;
EditText editText2;
EditText editText3;
TextView textViewResult;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parentLinearLayout = (LinearLayout) findViewById(R.id.parent_linear_layout);
editText1 = (EditText) findViewById(R.id.number_edit_text1);
editText2 = (EditText) findViewById(R.id.number_edit_text2);
editText3 = (EditText) findViewById(R.id.number_edit_text3);
textViewResult = (TextView) findViewById(R.id.number_text_view);
editText1.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textViewResult.setText(avg());
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
editText2.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textViewResult.setText(avg());
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
editText3.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textViewResult.setText(avg());
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
private String avg() {
int number1;
int number2;
int number3;
if(editText1.getText().toString() != "" && editText1.getText().length() > 0) {
number1 = Integer.parseInt(editText1.getText().toString());
} else {
number1 = 0;
}
if(editText2.getText().toString() != "" && editText2.getText().length() > 0) {
number2 = Integer.parseInt(editText2.getText().toString());
} else {
number2 = 0;
}
if(editText3.getText().toString() != "" && editText3.getText().length() > 0) {
number3 = Integer.parseInt(editText3.getText().toString());
} else {
number3 = 0;
}
return Integer.toString((number1 + number2 + number3)/3 );
}
public void onAddField(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field, null);
// Add the new row before the add field button.
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
}
public void onDelete(View v) {
parentLinearLayout.removeView((View) v.getParent());
}
}
两个.xml文件: activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent_linear_layout"
android:layout_width="match_parent"
android:layout_height="495dp"
android:layout_margin="0dp"
android:orientation="vertical"
android:weightSum="1">
<TableLayout
android:id="@+id/tlTable01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCC"
android:orientation="vertical"
android:paddingTop="0dp"
android:theme="@style/AppTheme">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7CFC00"
android:gravity="center_horizontal"
android:paddingBottom="1dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7CFC00"
android:gravity="left"
android:padding="5dp"
android:text="Scoring List"
android:textSize="18dp"
android:textStyle="bold"
android:typeface="serif" />
<EditText
android:layout_width="190dp"
android:layout_height="match_parent"
android:layout_marginLeft="1dp"
android:background="#FFF"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZαβγδεζηθικλμνξοπρστυφχψωΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ"
android:gravity="center_horizontal"
android:inputType="text"
android:padding="5dp"
android:text="Examiner's Name"
android:textSize="18dp"
android:textStyle="bold"
android:typeface="serif" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_weight="0.01"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:gravity="center_horizontal"
android:text="Student's Name"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:gravity="center_horizontal"
android:text="User Interface"
android:textStyle="bold"
android:typeface="serif"
android:layout_marginLeft="5dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:gravity="center_horizontal"
android:text="Functions"
android:textStyle="bold"
android:typeface="serif"
android:layout_marginLeft="5dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:gravity="center_horizontal"
android:text="User Manual"
android:textStyle="bold"
android:typeface="serif"
android:layout_marginLeft="5dp"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:gravity="center_horizontal"
android:text="AVG"
android:textStyle="bold"
android:typeface="serif"/>
<TextView
android:layout_height="match_parent"
android:layout_weight="2.35"
android:gravity="left"
android:layout_marginLeft="5dp"
android:layout_width="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="0.01"
android:orientation="horizontal">
<EditText
android:id="@+id/word_edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZαβγδεζηθικλμνξοπρστυφχψωΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ"
android:gravity="center_horizontal"
android:inputType="text" />
<EditText
android:id="@+id/number_edit_text1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:gravity="center_horizontal"
android:inputType="number" />
<EditText
android:id="@+id/number_edit_text2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:gravity="center_horizontal"
android:inputType="number" />
<EditText
android:id="@+id/number_edit_text3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:gravity="center_horizontal"
android:inputType="number" />
<TextView
android:id="@+id/number_text_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:gravity="center_horizontal"
android:textSize="18dp"
android:textStyle="bold"
android:typeface="serif"
android:layout_marginTop="9dp"/>
<Button
android:id="@+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="2.15"
android:background="@android:drawable/ic_delete"
android:onClick="onDelete" />
</LinearLayout>
<Button
android:id="@+id/add_field_button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#555"
android:onClick="onAddField"
android:paddingLeft="5dp"
android:text="Add Field"
android:textColor="#FFF" />
</LinearLayout>
和field.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_weight="0.01">
<EditText
android:id="@+id/number_edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="textCapWords"
android:gravity="center_horizontal" />
<EditText
android:id="@+id/number_edit_text1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="textCapWords"
android:gravity="center_horizontal" />
<EditText
android:id="@+id/number_edit_text2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="textCapWords"
android:gravity="center_horizontal" />
<EditText
android:id="@+id/number_edit_text3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="textCapWords"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/number_text_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:gravity="center_horizontal"
android:textSize="18dp"
android:textStyle="bold"
android:typeface="serif"
android:layout_marginTop="9dp"/>
<Button
android:id="@+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="2.15"
android:background="@android:drawable/ic_delete"
android:onClick="onDelete" />
</LinearLayout>
提前谢谢!
答案 0 :(得分:1)
以下是适合您的工作代码:
public void onAddField(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field, null);
((EditText) rowView.findViewById(R.id.number_edit_text1)).addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
((TextView) rowView.findViewById(R.id.number_text_view)).setText(
avg(((EditText) rowView.findViewById(R.id.number_edit_text1)),
((EditText) rowView.findViewById(R.id.number_edit_text2)),
((EditText) rowView.findViewById(R.id.number_edit_text3)))); }
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
((EditText) rowView.findViewById(R.id.number_edit_text2)).addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
((TextView) rowView.findViewById(R.id.number_text_view)).setText(
avg(((EditText) rowView.findViewById(R.id.number_edit_text1)),
((EditText) rowView.findViewById(R.id.number_edit_text2)),
((EditText) rowView.findViewById(R.id.number_edit_text3))));
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
((EditText) rowView.findViewById(R.id.number_edit_text3)).addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
((TextView) rowView.findViewById(R.id.number_text_view)).setText(
avg(((EditText) rowView.findViewById(R.id.number_edit_text1)),
((EditText) rowView.findViewById(R.id.number_edit_text2)),
((EditText) rowView.findViewById(R.id.number_edit_text3))));
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
// Add the new row before the add field button.
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
}
private String avg(EditText viewById, EditText viewById1, EditText viewById2) {
int number1;
int number2;
int number3;
if (viewById.getText().toString() != "" && viewById.getText().length() > 0) {
number1 = Integer.parseInt(viewById.getText().toString());
} else {
number1 = 0;
}
if (viewById1.getText().toString() != "" && viewById1.getText().length() > 0) {
number2 = Integer.parseInt(viewById1.getText().toString());
} else {
number2 = 0;
}
if (viewById2.getText().toString() != "" && viewById2.getText().length() > 0) {
number3 = Integer.parseInt(viewById2.getText().toString());
} else {
number3 = 0;
}
return Integer.toString((number1 + number2 + number3) / 3);
}
它没有用,因为你没有为这些字段分配任何监听器。
此外,我建议您使用 RecyclerView 作为您要添加的视图。
注意强>
avg
函数是新函数,不要用你的函数替换它。
希望有所帮助:)
答案 1 :(得分:1)
您还需要在新行edittexts中添加TextWatcher
。无需添加textwatcher
您无法找到他们的平均值
检查以下代码,我希望它对您有所帮助:
private String avg(EditText editText1,
EditText editText2,
EditText editText3) {
int number1;
int number2;
int number3;
if (editText1.getText().toString() != "" && editText1.getText().length() > 0) {
number1 = Integer.parseInt(editText1.getText().toString());
} else {
number1 = 0;
}
if (editText2.getText().toString() != "" && editText2.getText().length() > 0) {
number2 = Integer.parseInt(editText2.getText().toString());
} else {
number2 = 0;
}
if (editText3.getText().toString() != "" && editText3.getText().length() > 0) {
number3 = Integer.parseInt(editText3.getText().toString());
} else {
number3 = 0;
}
return Integer.toString((number1 + number2 + number3) / 3);
}
private void addTextListeners(EditText editText1,
EditText editText2,
EditText editText3,
TextView textViewResult) {
editText1.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textViewResult.setText(avg(editText1, editText2, editText3));
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
editText2.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textViewResult.setText(avg(editText1, editText2, editText3));
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
editText3.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textViewResult.setText(avg(editText1, editText2, editText3));
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
public void onAddField(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field, null);
// Add the new row before the add field button.
EditText editText1 = (EditText) rowView.findViewById(R.id.number_edit_text1);
EditText editText2 = (EditText) rowView.findViewById(R.id.number_edit_text2);
EditText editText3 = (EditText) rowView.findViewById(R.id.number_edit_text3);
TextView textViewResult = (TextView) findViewById(R.id.number_text_view);
addTextListeners(editText1, editText2, editText3, textViewResult);
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
}
1)从创建时删除文本更改侦听器,然后调用
addTextListeners(editText1, editText2, editText3, textViewResult);
2)使用上述方法更新了您的onAddField(v)
和avg()