android – 在AlertDialog中嵌入RatingBar

android – 在AlertDialog中嵌入RatingBar,第1张

概述我有一个ListView包含RatingBar来表示一些东西,如果你单击LisView的项目它d出一个包含RatingBar的Dialog(AlertDialog),现在我想知道: >如何自定义Dialog中显示的RatingBar(下面的代码不起作用,我认为这是最终的,这使得它无法定制). >如何将评级从Dialog RatingBar传递到ListView RatingBar. 我目前的代码是 我有一个ListVIEw包含ratingbar来表示一些东西,如果你单击lisVIEw的项目它d出一个包含ratingbar的Dialog(AlertDialog),现在我想知道:

>如何自定义Dialog中显示的ratingbar(下面的代码不起作用,我认为这是最终的,这使得它无法定制).
>如何将评级从Dialog ratingbar传递到ListVIEw ratingbar.

我目前的代码是:

final ratingbar ratingbar = new ratingbar(getApplicationContext());ratingbar.setrating(0);ratingbar.setStepSize(1);ratingbar.setNumStars(2);builder.setTitle(R.string.alertdialog_Title).setIcon(androID.R.drawable.ic_dialog_info).setVIEw(ratingbar).setPositivebutton("submit",new DialogInterface.OnClickListener() {    public voID onClick(DialogInterface dialog,int which) {        // do something.    }})              .setNegativebutton("Cancel",null);

EDIT1
我已经使用filelistadapter(fileListAdapater的一个实例作为code1)显示了ListvIEw项的评级.现在的问题是:

>单击对话框后评级会更改,但不是我单击的项目,如何映射具有评级(对话框)更改的项目.
>如何存储评级,每次ListVIEw刷新时,评级都会消失.

我已将onItemClickListener上的对话框代码作为code2.
代码1:

public class filelistadapter extends BaseAdapter {private Context context;private ArrayList<file> files; // The ArrayList to store the filesprivate boolean isSDcard; // To indicate whether the file is the SDcardprivate LayoutInflater mInflater; // The LayoutInflaterpublic filelistadapter (Context context,ArrayList<file> files,boolean isSDcard) {    this.context = context;    this.files = files;    this.isSDcard = isSDcard;    mInflater = LayoutInflater.from(context);}@OverrIDepublic int getCount () {    return files.size();}@OverrIDepublic Object getItem (int position) {    return files.get(position);}@OverrIDepublic long getItemID (int position) {    return position;}@OverrIDepublic VIEw getVIEw (int position,VIEw convertVIEw,VIEwGroup parent) {    VIEwHolder vIEwHolder;    /**     * The vIEw is not initialized,initialize the vIEw according to the layout file,* and bind it to tag.     */    if(convertVIEw == null) {         vIEwHolder = new VIEwHolder();        convertVIEw = mInflater.inflate(R.layout.file_List_item,null); // Inflate from the layout file        convertVIEw.setTag(vIEwHolder);        vIEwHolder.file_Title = (TextVIEw) convertVIEw.findVIEwByID(R.ID.file_Title); // Find the file_Title textvIEw        vIEwHolder.file_icon = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.file_icon); // Find the file_icon imagevIEw        vIEwHolder.file_path = (TextVIEw) convertVIEw.findVIEwByID(R.ID.file_path); // Find the file_path textvIEw        vIEwHolder.encrypt_level = (ratingbar) convertVIEw.findVIEwByID(R.ID.encrypt_level); // Find the rating_bar vIEw    } else {          vIEwHolder = (VIEwHolder) convertVIEw.getTag();    }    file file = (file) getItem(position);    if(position == 0 && !isSDcard) { // Add the back to SDcard item        vIEwHolder.file_Title.setText(R.string.back_to_sdcard);        vIEwHolder.file_icon.setimageResource(R.drawable.sdcard_icon);        vIEwHolder.file_path.setText(Environment.getExternalStorageDirectory().toString());        vIEwHolder.encrypt_level.setrating(0);    }    if(position == 1 && !isSDcard) { // Add the back to parent item        vIEwHolder.file_Title.setText(R.string.back_to_parent);        vIEwHolder.file_icon.setimageResource(R.drawable.folder_up_icon);        vIEwHolder.file_path.setText(file.getPath());        vIEwHolder.encrypt_level.setrating(0);    } else { // The current filepath is the SDcard or the position is neither 0 nor 1          String filename = file.getname();          vIEwHolder.file_Title.setText(filename);          vIEwHolder.file_path.setText(file.getPath());          vIEwHolder.encrypt_level.setrating(0);          if(file.isDirectory()) { // The variable file is a directory              vIEwHolder.file_icon.setimageResource(R.drawable.folder_icon);          } else { // The variable file is a file              vIEwHolder.file_icon.setimageResource(R.drawable.file_icon);          }      } return convertVIEw;}    private class VIEwHolder {        private ImageVIEw file_icon; // The icon of the file    private TextVIEw file_Title; // The Title of the file    private TextVIEw file_path; // The path of the file    private ratingbar encrypt_level; // The encrypt level of the file    }}

码2:

final Dialog rankDialog = new Dialog(MainActivity.this,R.style.FullHeightDialog);rankDialog.setContentVIEw(R.layout.rank_dialog);rankDialog.setCancelable(true);final ratingbar ratingbar = (ratingbar)rankDialog.findVIEwByID(R.ID.dialog_ratingbar);TextVIEw text = (TextVIEw) rankDialog.findVIEwByID(R.ID.rank_dialog_text1);text.setText("Set rating!");button updatebutton = (button) rankDialog.findVIEwByID(R.ID.rank_dialog_button);updatebutton.setonClickListener(new VIEw.OnClickListener() {    @OverrIDe    public voID onClick(VIEw v) {        Log.d(Tag,ratingbar.getrating() + "");        ratingbar ratebar = (ratingbar) findVIEwByID(R.ID.encrypt_level); // The ratingbar in the ListVIEw.    ratebar.setrating(ratingbar.getrating()); // Change the rating.    rankDialog.dismiss();}});rankDialog.show();
解决方法 尝试使用评级栏的自定义对话框

喜欢

button rankBtn = (button) findVIEwByID(R.ID.rank_button);rankBtn.setonClickListener(new VIEw.OnClickListener() {     public voID onClick(VIEw v) {        rankDialog = new Dialog(MyActivity.this,R.style.FullHeightDialog);        rankDialog.setContentVIEw(R.layout.rank_dialog);        rankDialog.setCancelable(true);        ratingbar = (ratingbar)rankDialog.findVIEwByID(R.ID.dialog_ratingbar);        ratingbar.setrating(userRankValue);        TextVIEw text = (TextVIEw) rankDialog.findVIEwByID(R.ID.rank_dialog_text1);        text.setText(name);        button updatebutton = (button) rankDialog.findVIEwByID(R.ID.rank_dialog_button);        updatebutton.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                rankDialog.dismiss();            }        });        //Now that the dialog is set up,it's time to show it            rankDialog.show();                    }});

这是layout / rank_dialog.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:orIEntation="vertical"    androID:padding="2dip">       <TextVIEw androID:text="Doctor's name" androID:ID="@+ID/rank_dialog_text1"         androID:textSize="24dp" androID:layout_margintop="10dp"         androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"          androID:layout_gravity="center"/>     <ratingbar          androID:layout_margintop="10dp" androID:layout_marginBottom="10dp"         androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"         androID:clickable="true"         androID:ID="@+ID/dialog_ratingbar" androID:layout_gravity="center"         androID:numStars="5" androID:stepSize="1.0" androID:rating="2"         androID:isIndicator="false"/>     <button androID:ID="@+ID/rank_dialog_button"          androID:layout_height="wrap_content"          androID:text="Update"  androID:focusable="false"         androID:layout_margin="5dip" androID:textcolor="#FFF8C6"         androID:textSize="20dip" androID:layout_wIDth="fill_parent" /></linearLayout>

值/ my_style.xml

<?xml version="1.0" enCoding="utf-8"?><resources>  <style name="FullHeightDialog" parent="androID:style/theme.Dialog">  <item name="androID:windowNoTitle">true</item>  </style></resources>

如需更多帮助,请访问此tutorial

总结

以上是内存溢出为你收集整理的android – 在AlertDialog中嵌入RatingBar全部内容,希望文章能够帮你解决android – 在AlertDialog中嵌入RatingBar所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1141130.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存