0

我有一個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(); 
} 

預先感謝您。

+0

我也遇到過這個問題,不知道爲什麼....如果使用dialog.dismiss(); ? – Opiatefuchs

+0

謝謝@Opiatefuchs。它沒有任何作用(使用dialog.dismiss())。 –

+0

'return builder.create()'...你在方法裏面創建對話框嗎?你沒有發佈那個... – Opiatefuchs

回答

1

在您的onMenuItemActionExpand()中設置日誌命令。我敢打賭,你會看到這種方法是在你解散對話之後調用的。很可能,一旦焦點從對話返回到活動,系統就會調用此方法。您應該撥打dialog.show(),而不是onOptionsItemSelected(MenuItem item)

+0

謝謝@JCricket。這可能是問題的起因。我將測試日誌輸出。 –

+0

謝謝@JCricket。工作! –