如何在初始构建后重置对 DialogFragment 的输入?
How to reset input to DialogFramgment after initial build?
编辑:附上我的 onCickListener 的代码,它将评级值发送到然后显示我的对话框。
我有一个 TextView,它以数字格式 (4.5) 显示评分。当我按下这个 TextView 时,会弹出一个对话框让我通过 RatingBar 更改评级。 Ratingbar 的评级在弹出时设置为等于 TextView Rating。这按预期运行,当我按 OK 时,TextView 更新为新评级。但是,当我再次按下 TextView 时,会显示初始的第一个值,而不是我刚刚将其更新为的值。我已经弄明白了,因为我的所有代码都在 onCreateDialog () 中。我试图通过使用 OnStart() 和 onResume() 来让它工作,但后来我的应用程序崩溃了。如何正确编写此代码?
附件是我的功能代码,所有代码都在 onCreadeDialog()
中设置
public class RatingDialog extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
final View DialogView = inflater.inflate(R.layout.dialog_rating, null);
/**
* Retrieve the argument "num" (Previously rating) and set ratingbar´s rating equal to this.
*/
getArguments().getFloat("num");
RatingBar ValueView = (RatingBar) DialogView.findViewById(R.id.Ratingbar);
ValueView.setRating(getArguments().getFloat("num"));
builder.setView(DialogView)
// Add action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
/**
* Get the new value from the Ratingbar and send this back to the AddRating TextView
*/
RatingBar ValueView = (RatingBar) DialogView.findViewById(R.id.Ratingbar);
float Value = ValueView.getRating();
TextView Text = (TextView) getActivity().findViewById(R.id.AddRating);
Text.setText(String.valueOf(Value));
RatingDialog.this.getDialog().cancel();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
RatingDialog.this.getDialog().cancel();
}
});
return builder.create();
}
}
下面是 onCickListener 的代码,它将评级值发送到然后显示我的对话框:
/**
* Set the On Click Listener and send the Rating value to the Dialog
*/
final TextView Rating = (TextView) findViewById(R.id.AddRating);
String S = (String) Rating.getText();
final Float F;
if (S==""){
F=0.0f;}
else
F = Float.valueOf(S);
Rating.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RatingDialog newFragment = new RatingDialog();
newFragment.show(getSupportFragmentManager(), "Rating");
/**
* Send Verdien av rating til dialogvinduet
*/
Bundle args = new Bundle();
args.putFloat("num", F);
newFragment.setArguments(args);
}
});
您一直在传递相同的 F
值。
应该是:
final TextView rating = (TextView) findViewById(R.id.AddRating);
rating.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RatingDialog newFragment = new RatingDialog();
newFragment.show(getSupportFragmentManager(), "Rating");
/**
* Send Verdien av rating til dialogvinduet
*/
Bundle args = new Bundle();
args.putFloat("num", TextUtils.isEmpty(rating.getText()) ?
0.0f : Float.valueOf(rating.getText().toString()));
newFragment.setArguments(args);
}
});
在这种情况下,您将传递 rating
TextView 的实际值。
是的,请关注java code convention。因为很难阅读你的代码。
编辑:附上我的 onCickListener 的代码,它将评级值发送到然后显示我的对话框。 我有一个 TextView,它以数字格式 (4.5) 显示评分。当我按下这个 TextView 时,会弹出一个对话框让我通过 RatingBar 更改评级。 Ratingbar 的评级在弹出时设置为等于 TextView Rating。这按预期运行,当我按 OK 时,TextView 更新为新评级。但是,当我再次按下 TextView 时,会显示初始的第一个值,而不是我刚刚将其更新为的值。我已经弄明白了,因为我的所有代码都在 onCreateDialog () 中。我试图通过使用 OnStart() 和 onResume() 来让它工作,但后来我的应用程序崩溃了。如何正确编写此代码?
附件是我的功能代码,所有代码都在 onCreadeDialog()
中设置public class RatingDialog extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
final View DialogView = inflater.inflate(R.layout.dialog_rating, null);
/**
* Retrieve the argument "num" (Previously rating) and set ratingbar´s rating equal to this.
*/
getArguments().getFloat("num");
RatingBar ValueView = (RatingBar) DialogView.findViewById(R.id.Ratingbar);
ValueView.setRating(getArguments().getFloat("num"));
builder.setView(DialogView)
// Add action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
/**
* Get the new value from the Ratingbar and send this back to the AddRating TextView
*/
RatingBar ValueView = (RatingBar) DialogView.findViewById(R.id.Ratingbar);
float Value = ValueView.getRating();
TextView Text = (TextView) getActivity().findViewById(R.id.AddRating);
Text.setText(String.valueOf(Value));
RatingDialog.this.getDialog().cancel();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
RatingDialog.this.getDialog().cancel();
}
});
return builder.create();
}
}
下面是 onCickListener 的代码,它将评级值发送到然后显示我的对话框:
/**
* Set the On Click Listener and send the Rating value to the Dialog
*/
final TextView Rating = (TextView) findViewById(R.id.AddRating);
String S = (String) Rating.getText();
final Float F;
if (S==""){
F=0.0f;}
else
F = Float.valueOf(S);
Rating.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RatingDialog newFragment = new RatingDialog();
newFragment.show(getSupportFragmentManager(), "Rating");
/**
* Send Verdien av rating til dialogvinduet
*/
Bundle args = new Bundle();
args.putFloat("num", F);
newFragment.setArguments(args);
}
});
您一直在传递相同的 F
值。
应该是:
final TextView rating = (TextView) findViewById(R.id.AddRating);
rating.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RatingDialog newFragment = new RatingDialog();
newFragment.show(getSupportFragmentManager(), "Rating");
/**
* Send Verdien av rating til dialogvinduet
*/
Bundle args = new Bundle();
args.putFloat("num", TextUtils.isEmpty(rating.getText()) ?
0.0f : Float.valueOf(rating.getText().toString()));
newFragment.setArguments(args);
}
});
在这种情况下,您将传递 rating
TextView 的实际值。
是的,请关注java code convention。因为很难阅读你的代码。