2015-12-30 84 views
2

我做任務返回值,我需要點擊EditText後顯示一個對話框。在該對話框中,我使用RecyclerView顯示RadioButton的內容。從DialogFragment

現在,我想要做的是,從對話框中選擇RadioButton(在RecyclerView中的內容)後,它應該返回該內容的值,然後對話框應該被取消。

要生成我用一個DialogFragment的對話框。

正如我在android開發新的,我完全糊塗了,無法找到解決方案。

+0

發表您的問題代碼.. – Mohit

+0

@Mohit其實,有一個在代碼中沒有錯誤,我摸不清了解如何實現這一點。 –

+0

我把類作爲ldDialog.java,ldListAdapter.java而顯示對話框的代碼是「editTextLeadSource.setOnClickListener(new View。OnClickListener(){ @Override public void onClick(View v){ launchDialog(「LeadSource」); });「 –

回答

12

因爲你的對話框是一個DialogFragment你可以用兩件事情

  1. 如果從一個Activity開始的對話框中,你可以使用一個接口
  • 創建一個接口

    public interface ISelectedData { 
        void onSelectedData(String string); 
    } 
    
  • 實現您的活動的界面在你的對話框

    public class MyActivity implements ISelectedData { 
    
        ..... 
    
        @Override 
        public void onSelectedData(String myValue) { 
         // Use the returned value 
        } 
    } 
    
  • ,將接口到你的活動

    private ISelectedData mCallback; 
    
    @Override 
    public void onAttach(Activity activity) { 
        super.onAttach(activity); 
    
        try { 
         mCallback = (ISelectedData) activity; 
        } 
        catch (ClassCastException e) { 
         Log.d("MyDialog", "Activity doesn't implement the ISelectedData interface"); 
        } 
    } 
    
    返回值到活動時
  • ,只需調用在對話框

    mCallback.onSelectedData("myValue"); 
    

    檢查example在Android下發展r網站。

  • 如果從一個Fragment開始對話,可以使用setTargetFragment(...)
    • 啓動對話

      MyDialog dialog = new MyDialog(); 
      dialog.setTargetFragment(this, Constants.DIALOG_REQUEST_CODE); 
      dialog.show(fragmentManager, Constants.DIALOG); 
      
    • 從對話框

      Bundle bundle = new Bundle(); 
      bundle.putString(Constants.MY_VALUE, "MyValue"); 
      
      Intent intent = new Intent().putExtras(bundle); 
      
      getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent); 
      
      dismiss(); 
      
    • 片段中獲取值返回值

      @Override 
      public void onActivityResult(int requestCode, int resultCode, Intent data) { 
          super.onActivityResult(requestCode, resultCode, data); 
      
          if (requestCode == Constants.DIALOG_REQUEST_CODE) { 
      
           if (resultCode == Activity.RESULT_OK) { 
      
            if (data.getExtras().containsKey(Constants.MY_VALUE)) { 
      
             String myValue = data.getExtras().getString(Constants.MY_VALUE); 
      
             // Use the returned value 
            } 
           } 
          } 
      }  
      
    +0

    我怎樣才能設置點擊事件到該對話框內的recyclerview的內容,以便在該點擊事件中,我獲取值並設置爲Edittextview。 –

    +0

    如果您需要點擊監聽你的RecyclerView的行,你可以使用類似[this](http://stackoverflow.com/questions/24885223/why-doesnt-recyclerview-have-onitemclicklistener-and-how-recyclerview-is-dif)。在行內查看或使用277 upvotes的答案[這裏](http://stackoverflow.com/questions/24471109/recyclerview-onclick)。 – Marko

    +0

    謝謝哥們,我會試試這個解決方案... @ Marko –