在AlertDialog中嵌入RatingBar

 28划生12_928 发布于 2023-02-08 15:42

我有一个ListView包含RatingBar来表示一些东西,如果你单击LisView的项目它弹出一个包含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 files; // The ArrayList to store the files
private boolean isSDcard; // To indicate whether the file is the SDcard
private LayoutInflater mInflater; // The LayoutInflater

public FileListAdapter (Context context, ArrayList files, boolean isSDcard) {
    this.context = context;
    this.files = files;
    this.isSDcard = isSDcard;
    mInflater = LayoutInflater.from(context);
}

@Override
public int getCount () {
    return files.size();
}

@Override
public Object getItem (int position) {
    return files.get(position);
}

@Override
public long getItemId (int position) {
    return position;
}

@Override
public 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();

Praveen Shar.. 12

尝试使用评级栏的自定义对话框

喜欢

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


  

     

     

     

值/ my_style.xml



  

有关更多帮助,请访问本教程

1 个回答
  • 尝试使用评级栏的自定义对话框

    喜欢

    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_
        android:layout_
        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_ android:layout_ 
             android:layout_gravity="center"/>
    
         <RatingBar 
             android:layout_marginTop="10dp" android:layout_marginBottom="10dp"
             android:layout_ android:layout_
             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_ 
             android:text="Update"  android:focusable="false"
             android:layout_margin="5dip" android:textColor="#FFF8C6"
             android:textSize="20dip" android:layout_ />
    
    </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>
    

    有关更多帮助,请访问本教程

    2023-02-08 15:44 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有