2012-07-29 84 views

回答

0

下面是一些代码,我在一个应用程序用于显示一个对话框片段,并获得选择了它:

public class FeedChooserFragment extends DialogFragment { 

    /** 
    * Implement this interface if the activity needs to do something 
    * after the dialog has been dismissed. 
    */ 
    public interface FeedChooserListener { 
     public void onFeedSelected(NewsFeed feed, Object userData); 
    } 

    /** 
    * Create a new instance of the fragment 
    */ 
    public static FeedChooserFragment newInstance(Serializable userData) { 
     FeedChooserFragment f = new FeedChooserFragment(); 

     Bundle args = new Bundle(); 
     args.putSerializable(Extra.USER_DATA, userData); 
     f.setArguments(args); 

     return f; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // This is a list of items, but it could be a custom dialog showing some rating bar 
     BaseActivity a = (BaseActivity) getActivity(); 
     List<NewsFeed> feed = a.getDataCache().getAllNewsFeed(); 
     adapter = new FeedAdapter(a); 
     adapter.addAll(feed); 

     // Here you would create a custom dialog, find the rating bar in the inflated view 
     // and keep it ready for when the dialog gets dismissed. 
     AlertDialog.Builder builder = new AlertDialog.Builder(a); 

     // Here you would set the button listeners instead of the listview listener 
     builder.setAdapter(adapter, dialogClickListener); 
     return builder.create(); 
    } 

    private OnClickListener dialogClickListener = new OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      NewsFeed selectedFeed = adapter.getItem(which); 

      // This is where we try to notify the calling activity or fragment 
      if (getActivity() instanceof FeedChooserListener) { 
       ((FeedChooserListener) getActivity()).onFeedSelected(selectedFeed, userData); 
      } 

      if (getTargetFragment() instanceof FeedChooserListener) { 
       ((FeedChooserListener) getTargetFragment()).onFeedSelected(selectedFeed, userData); 
      } 

      dialog.dismiss(); 
     } 
    }; 

    private Object userData; 
    private FeedAdapter adapter; 
} 
+0

谢谢,我会试试这个。 – JetPro 2012-07-29 09:51:04

0

是的,它可以,请尝试这样的事情...

PopupWindow pw;  
//We need to get the instance of the LayoutInflater, use the context of this activity 
LayoutInflater inflater = (LayoutInflater) TouchPaint.this 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
//Inflate the view from a predefined XML layout 
View layout = inflater.inflate(R.layout.popup, 
     (ViewGroup) findViewById(R.id.popup_element)); 

//popup : name of the XML file which includes the popup_element(can be a linear layout which includes the rating bar) 

pw = new PopupWindow(layout,70, 220, true); 
pw.showAtLocation(layout, Gravity.LEFT,100,200); 
rb =(RatingBar)layout. findViewById(R.id.RatingBar);  
rb.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     // To Do code 
        pw.dismiss(); 
    } 
}); 
相关问题