评级栏未显示在警告对话框中

时间:2017-01-13 13:07:13

标签: java android android-layout

我正在创建一个应用,因为我允许客户对该应用进行评分。

我有两个问题

第一

我在Alert对话框中添加了评级栏,但不知道为什么它会覆盖我创建用于提交评级的按钮。 我已尝试使用margin-top按钮仍无法正常工作

Here is the image of alert dialog

第二

在提交时,它也没有显示Toast消息。

警报对话代码

if (id == R.id.nav_rate) {
        AlertDialog.Builder mBuild = new AlertDialog.Builder(homeActivity.this);
        View mView = getLayoutInflater().inflate(R.layout.ratingbar,null);
        final RatingBar ratebar = (RatingBar)mView.findViewById(R.id.ratingBar);
        Button btnSubmit=(Button)mView.findViewById(R.id.btnSubRating);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            ratebar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
                @Override
                public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                    Toast.makeText(homeActivity.this, ((int) rating),Toast.LENGTH_LONG).show();
                }
            });
            }
        });
        mBuild.setView(mView);
        AlertDialog dialog=mBuild.create();
        dialog.show();
    }

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">

<RatingBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ratingBar"
    android:numStars="5"
    android:rating="1"
    android:stepSize="0.5"
    android:layout_above="@+id/btnSubRating"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="48dp"
    android:layout_gravity="center_horizontal" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit"
    android:id="@+id/btnSubRating"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal" />

3 个答案:

答案 0 :(得分:1)

public class homeActivity extends AppCompatActivity  {


    private float rateValue;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);


            AlertDialog.Builder mBuild = new AlertDialog.Builder(homeActivity.this);
            View mView = getLayoutInflater().inflate(R.layout.ratingbar,null);

            final RatingBar ratebar = (RatingBar)mView.findViewById(R.id.ratingBar);
            ratebar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
                @Override
                public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                    rateValue = rating;
                    Toast.makeText(homeActivity.this, ""+rating, Toast.LENGTH_SHORT).show();
                }
            });

            Button btnSubmit=(Button)mView.findViewById(R.id.btnSubRating);
            btnSubmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(homeActivity.this, ""+rateValue, Toast.LENGTH_SHORT).show();
                }
            });

            mBuild.setView(mView);
            AlertDialog dialog=mBuild.create();
            dialog.show();

}

}

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/rate_parent"
    android:weightSum="10">

    <RatingBar
        android:layout_marginBottom="30dp"
        android:layout_weight="5"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:id="@+id/ratingBar"
        android:numStars="5"
        android:rating="1"
        android:stepSize="0.5"
        android:layout_above="@+id/btnSubRating"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal" />

    <Button
        android:layout_weight="5"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="Submit"
        android:id="@+id/btnSubRating"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal" />

    </LinearLayout>

答案 1 :(得分:0)

你已经修改了不允许评级栏适合的对话框大小。

替换dialog.getWindow().setLayout(600, 400);

dialog.getWindow().setLayout(800, 600);

在您的活动中

创建类变量: -

 float rating=0;

并使用它来存储评级点击次数

    AlertDialog.Builder mBuild = new AlertDialog.Builder(MainActivity.this);
    View mView = getLayoutInflater().inflate(R.layout.ratingbar,null);
    final RatingBar ratebar = (RatingBar)mView.findViewById(R.id.ratingBar);
    Button btnSubmit=(Button)mView.findViewById(R.id.btnSubRating);

ratebar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
    @Override
    public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
        rating=v;
    }
});
    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(homeActivity.this, String.valueOf(ratebar.getRating()),Toast.LENGTH_LONG).show();
        }
    });
    mBuild.setView(mView);
    AlertDialog dialog=mBuild.create();
    dialog.getWindow().setLayout(600, 400);
    dialog.show();
}

答案 2 :(得分:0)

尝试:

AlertDialog alertDialog = mBuild.show();

代替:

AlertDialog dialog=mBuild.create();
dialog.getWindow().setLayout(600, 400);
dialog.show();

此外:android:layout_above只允许在RelativeLayout上(所以删除它)