【发布时间】:2016-06-07 09:20:06
【问题描述】:
在我的应用程序中,我有一个对话框片段,它在启动 MainActivity 之前首先打开。当在该对话框片段中输入一些数据并单击确认时,我需要将一些值(表示双精度值)发送到片段(主)。
我的主要活动包括视图寻呼机,它有两个片段。一个是片段(主)..我必须将数据发送到我的对话框片段到片段(主)
fragment(Main) 通过 viewpager 附加到 MainActivity。 我尝试使用 Intents 和 bundle 将值发送到片段(主)。 引用站点在片段之间传递数据。
虽然通过使用 Intents 显示异常无法显式您在 android Manifest 中声明
通过尝试使用 bundle 得到异常 bundle 为 null。
从这两个例外中我得到的是可以发送数据.. 任何机构都可以提供帮助并提供建议。
这是我的对话片段:
public class InputFragment extends DialogFragment {
public InputFragment() {
// Required empty public constructor
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return super.onCreateDialog(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.input, container, false);
final EditText ed = (EditText)view.findViewById(R.id.editText);
final RadioButton rb1 = (RadioButton)view.findViewById(R.id.radioButton1);
final RadioButton rb2 = (RadioButton)view.findViewById(R.id.radioButton2);
final Button b1 = (Button)view.findViewById(R.id.cancel);
final Button b2 = (Button)view.findViewById(R.id.confirm);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!ed.getText().toString().equals("")) {
if (rb1.isChecked()) {
int input = Integer.valueOf(ed.getText().toString());
double out = input / 24.0;
out = Double.parseDouble(new DecimalFormat("#.0").format(out));
Intent m = new Intent(getActivity().getBaseContext(), MainScreen.class);
m.putExtra("res", out);
getActivity().startActivity(m);
} else if (rb2.isChecked()) {
int input = Integer.valueOf(ed.getText().toString());
double out = ((input * 2 / 3) * 0.029574);
out = Double.parseDouble(new DecimalFormat("#.0").format(out));
Intent m = new Intent(getActivity().getBaseContext(), MainScreen.class);
m.putExtra("res", out);
getActivity().startActivity(m);
}
}
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
return view;
}
接收数据的片段(主):
public class MainScreen extends Fragment {
double ram;
TextView gls;
MainScreen mainScreen;
public MainScreen() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.main_screen, container, false);
mainScreen = this;
TextView textView = (TextView)view.findViewById(R.id.res);
Intent r = getActivity().getIntent();
double res = r.getDoubleExtra("res", 0);
String result = Double.toString(res);
textView.setText(result);
ram = Double.parseDouble(textView.getText().toString());
gls = (TextView) view.findViewById(R.id.glasses);
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DiaFragment df = new DiaFragment();
df.setCallingFragment(mainScreen);
df.show(getFragmentManager(),"Frag");
}
});
return view;
}
以上是我有意尝试的,
通过使用捆绑: 在我的片段中(主要)
Bundle mbundle = mainScreen.getArguments();
double res = mbundle.getDouble("res",0);
String result = Double.toString(res);
textView.setText(result);
对话片段:
InputFragment inputFragment = new InputFragment();
Bundle bundle = new Bundle();
bundle.putDouble("res",out);
inputFragment.setArguments(bundle);
【问题讨论】:
标签: android android-fragments android-dialogfragment