2017-01-06 44 views
1

我在一个片段中工作,我希望用户输入一个答案,然后当点击一个按钮时,一个文本视图应该显示正确。 这是我的代码Android studio EditText不加载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

您试图在按钮上查找textView TextView tv =(TextView)v.findViewById(R.id.showCorrect);' 意味着当您单击对象时onClick(View v)'获取您单击视图中的对象。所以你需要用你的userAnswer对象初始化 – user5599807

+0

@ user5599807你是什么意思用userAnswer对象初始化?你可以更清楚地了解我应该更改的代码 –

回答

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值始终是相同的(未选中的按钮点击)

+0

我将代码更改为此,但是当我按下提交时,崩溃了应用程序 –

+0

@KaraW发布您的错误请 –

+0

没有错误,无法获取堆栈跟踪显示任何内容。它在更改代码之前没有崩溃 –

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