我在片段中工作,我希望用户输入答案,然后单击按钮时,textview应显示正确。 这是我的代码
public class FragmentOne extends Fragment {
private EditText userAnswer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment_one, null);
userAnswer = (EditText)v.findViewById(R.id.editText);
final String theAnswer = (userAnswer.getText().toString());
Button submit = (Button)v.findViewById(R.id.submitBtn1);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (theAnswer.equalsIgnoreCase("Germany")) {
TextView tv = (TextView)v.findViewById(R.id.showCorrect);
tv.setText("Correct!");
}
}
});
return v;
}
}
然而,当输入德国时,textView仍然只显示' x' (我把它设置为最初显示)
相关的XML
<Button
android:text="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="@+id/submitBtn1"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="220dp"
android:layout_height="70dp"
android:inputType="textPersonName"
android:hint="Write answer and press enter"
android:ems="10"
android:id="@+id/editText"
android:textSize="16sp"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true" />
<TextView
android:text="x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/showCorrect"
android:layout_below="@+id/skipBtn"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
出了什么问题?
答案 0 :(得分:1)
您必须更改此行:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment_one, null);
userAnswer = (EditText)v.findViewById(R.id.editText);
TextView tv = (TextView)v.findViewById(R.id.showCorrect);
Button submit = (Button)v.findViewById(R.id.submitBtn1);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String theAnswer = (userAnswer.getText().toString());
if (theAnswer.equalsIgnoreCase("Germany")) {
tv.setText("Correct!");
}
}
});
return v;
}
即:在您的代码中,userAnswer值始终相同(未在按钮单击时选中)
答案 1 :(得分:1)
代码需要更改为:
public class FragmentOne extends Fragment {
private EditText userAnswer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment_one, null);
userAnswer = (EditText)v.findViewById(R.id.editText);
final TextView tv = (TextView)v.findViewById(R.id.showCorrect); //changed
Button submit = (Button)v.findViewById(R.id.submitBtn1);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String theAnswer = (userAnswer.getText().toString());
if (theAnswer.equalsIgnoreCase("Germany")) {
//TextView tv = (TextView)v.findViewById(R.id.showCorrect);
tv.setText("Correct!");
}
// updateScore();
}
});
return v;
}