我尝试在检查完所有rb后立即在Fragment41
显示文本。如何在Fragment41
中显示此文字?我试过几种方法,但我对这种实现感到困惑。我知道方法getIntent().
这里错了,但我不知道该改变它的方法。请帮助解决这一切。
我的Fragment41
package make.appaplication;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Fragment41 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_41, container, false);
TextView textViewDisplayResult = (TextView) getView().findViewById(R.id.text_view_display_result);
textViewDisplayResult.setText(getIntent().getBooleanExtra("KEY_ANSWER", false)?R.string.Good_answer:R.string.Wrong_answer);
}
}
Fragment41
FragmentList
MainActivity
fragmentList.add(new Fragment41());
以下是checkButton
buttonCheckAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkAnswers())
intent.putExtra("Key Answer", true);
else
intent.putExtra("Key Answer", false);
startActivity(intent);
}
});
还有一个问题
我有两个strings
"答案很好"和#34;错误答案"它们的代码是
buttonCheckAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkAnswers())
intent.putExtra("Key Answer", true);
else
intent.putExtra("Key Answer", false);
startActivity(intent);
}
});
如何添加上面和下面的代码,还有一个声明string
"您还没有检查过所有答案"?
textViewDisplayResult.setText(getActivity().getIntent().getBooleanExtra("Key Answer", false)?R.string.Good_answer:R.string.Wrong_answer);
答案 0 :(得分:0)
对来自"Key Answer"
的{{1}}和put
额外数据使用相同的KEY get
。
更新Intent
,如下所示
Fragment41
<强>更新强>
使用package make.appaplication;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Fragment41 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_41, container, false);
TextView textViewDisplayResult = (TextView) view.findViewById(R.id.text_view_display_result);
boolean isAnswered = getIntent().getBooleanExtra("is_answered");
if (isAnswered)
textViewDisplayResult.setText(getIntent().getBooleanExtra("Key Answer", false)?R.string.Good_answer:R.string.Wrong_answer);
else
textViewDisplayResult.setText("You haven't checked all answers");
return view;
}
变量boolean
检查天气用户是否回答。使用KEY isAnswered
将此值传递给片段。
is_answered
答案 1 :(得分:0)
您需要使用getActivity().getIntent().get...
而不是仅调用getIntent()
。所以片段中的整个代码是:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_41, container, false);
TextView textViewDisplayResult = (TextView) view.findViewById(R.id.text_view_display_result);
textViewDisplayResult.setText(getActivity().getIntent().getBooleanExtra("Key Answer", false)?R.string.Good_answer:R.string.Wrong_answer);
return view;
}
我想我已在你上一篇文章中回答过。
当然,我认为你做到了(如果你没有请先阅读文档https://developer.android.com/guide/components/fragments.html#Adding),你需要将你的片段添加到Activity:
getFragmentManager().beginTransaction().add(R.id.fragmentHolder, fragment_41_instance).addToBackStack(null).commit();
另一个问题
buttonCheckAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!allAnswersChecked())
intent.putExtra("Key Answer", 1);
else if (checkAnswers())
intent.putExtra("Key Answer", 2);
else
intent.putExtra("Key Answer", 3);
startActivity(intent);
}
});
然后:
int result = getActivity().getIntent().getIntExtra("Key Answer", 1);
textViewDisplayResult.setText(result == 1 ? R.string.text_not_checked :
result == 2 ? R.string.Good_answer : R.string.Wrong_answer);