点击按钮时打开评级栏

时间:2012-07-29 08:13:29

标签: android onclick ratingbar

单击按钮时,是否可以制作一个“AlertDialog-like display”,里面有评级栏?如果是,我能否获得输入的评级值?感谢。

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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();
    }
});