我有一個dialogfragment,它的視圖由一個簡單的seekbar和正面和負面的按鈕組成。當我點擊onclick dialogfragment時,會創建並顯示一個新的對話框片段。第二次點擊按鈕後,對話框被解除,但我的願望是第一次點擊後解散。另外我應該注意到我的活動主持了兩個片段,我從第一個片段調用dialog.show()。關閉DialogFragment onClick不能正常工作
MyListFragment.java
FragmentManager manager = getFragmentManager();
fndSrchFragment dialog = new fndSrchFragment();
dialog.setTargetFragment(MyListFragment.this, REQUEST_COLOR);
dialog.show(manager, SRCH_FND);
fndSrchFragment.java
public class fndSrchFragment extends DialogFragment {
.
.
//variable definition
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.color_picker, null);
rSb=(SeekBar) view.findViewById(R.id.r_seek_bar);
.
.
//code for interacting with seekbar
.
.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int ccolor = android.graphics.Color.rgb(rComponent, gComponent, bComponent);
sendResult(Activity.RESULT_OK, ccolor);
// two click is needed for dismiss which I don't know why?
dismiss();
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// two click is needed for dismiss which I don't know why?
dismiss();
}
});
return builder.create();
}
預先感謝您。
我也遇到過這個問題,不知道爲什麼....如果使用dialog.dismiss(); ? – Opiatefuchs
謝謝@Opiatefuchs。它沒有任何作用(使用dialog.dismiss())。 –
'return builder.create()'...你在方法裏面創建對話框嗎?你沒有發佈那個... – Opiatefuchs