评分栏未显示

huangapple go评论87阅读模式
英文:

Rating Bar not appearing

问题

以下是翻译好的部分:

我正在尝试在Android Studio中使用评分条,但它在我的应用程序中并未显示出来。
我没有收到任何错误,并且无法理解我做错了什么。
有其他人遇到过这个问题吗?

这是我的Java文件:

package com.example.myopenlounge;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.analytics.FirebaseAnalytics;

public class RatingActivity extends AppCompatActivity {

    private RatingBar mRatingBar;
    private TextView mRatingScale;
    private EditText mFeedback;
    private Button mSendFeedback;
    private int star1 = 0;
    private int star2 = 0;
    private int star3 = 0;
    private int star4 = 0;
    private int star5 = 0;

    public static final String TAG = "TAG";

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

        mRatingBar = (RatingBar) findViewById(R.id.ratingBar);
        mRatingScale = (TextView) findViewById(R.id.tvRatingScale);
        mFeedback = (EditText) findViewById(R.id.etFeedback);
        mSendFeedback = (Button) findViewById(R.id.btnSubmit);

        mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                mRatingScale.setText(String.valueOf(rating));
                switch((int) ratingBar.getRating()){
                    case 1:
                        mRatingScale.setText("糟糕!");
                        star1++;
                        break;
                    case 2:
                        mRatingScale.setText("不太好...");
                        star2++;
                        break;
                    case 3:
                        mRatingScale.setText("好");
                        star3++;
                        break;
                    case 4:
                        mRatingScale.setText("很棒");
                        star4++;
                        break;
                    case 5:
                        mRatingScale.setText("太棒了!");
                        star5++;
                        break;
                    default:
                        mRatingScale.setText("");
                }
            }
        });

        mSendFeedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mFeedback.getText().toString().isEmpty()){
                    Toast.makeText(RatingActivity.this, "请填写反馈文本框", Toast.LENGTH_LONG).show();
                }
                else{
                    mFeedback.setText("");
                    mRatingBar.setRating(0);
                    Toast.makeText(RatingActivity.this, "感谢您分享您的反馈 :)", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

然后我的XML文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RatingActivity"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center_horizontal"
        android:text="我们希望您今天在我们这里用餐愉快"
        android:textSize="18sp"
        android:textStyle="italic" />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:stepSize="1"
        android:rating="5" />

    <TextView
        android:id="@+id/tvRatingScale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:text="太棒了。我喜欢它"
        android:textSize="16sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/etFeedback"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:lines="5"
        android:hint="告诉我们为什么给了我们这个评分"
        android:gravity="top" />

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="#e57373"
        android:text="发送反馈"
        android:textColor="@android:color/white" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        tools:layout_editor_absoluteX="17dp"
        tools:layout_editor_absoluteY="46dp" />
</LinearLayout>

当调用onCreate方法时,屏幕上只显示出文本“我们希望您今天在我们这里用餐愉快”。我似乎找不到原因。

英文:

I am trying to use a rating bar in Android Studio but it just isn't appearing in my app.
I am not receiving any errors and can't understand what I am doing wrong.
Has anyone else experienced this issue?

Here is my java file;

    package com.example.myopenlounge;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.analytics.FirebaseAnalytics;
public class RatingActivity extends AppCompatActivity {
private RatingBar mRatingBar;
private TextView mRatingScale;
private EditText mFeedback;
private Button mSendFeedback;
private int star1 = 0;
private int star2 = 0;
private int star3 = 0;
private int star4 = 0;
private int star5 = 0;
public static final String TAG = &quot;TAG&quot;;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating);
mRatingBar = (RatingBar) findViewById(R.id.ratingBar);
mRatingScale = (TextView) findViewById(R.id.tvRatingScale);
mFeedback = (EditText) findViewById(R.id.etFeedback);
mSendFeedback = (Button) findViewById(R.id.btnSubmit);
mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
mRatingScale.setText(String.valueOf(rating));
switch((int) ratingBar.getRating()){
case 1:
mRatingScale.setText(&quot;Awful!&quot;);
star1++;
break;
case 2:
mRatingScale.setText(&quot;Not great...&quot;);
star2++;
break;
case 3:
mRatingScale.setText(&quot;Good&quot;);
star3++;
break;
case 4:
mRatingScale.setText(&quot;Great&quot;);
star4++;
break;
case 5:
mRatingScale.setText(&quot;Fantastic!&quot;);
star5++;
break;
default:
mRatingScale.setText(&quot;&quot;);
}
}
});
mSendFeedback.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mFeedback.getText().toString().isEmpty()){
Toast.makeText(RatingActivity.this, &quot;Please fill in feedback text box&quot;, 
Toast.LENGTH_LONG).show();
}
else{
mFeedback.setText(&quot;&quot;);
mRatingBar.setRating(0);
Toast.makeText(RatingActivity.this, &quot;Thank you for sharing your feedback :) &quot;, 
Toast.LENGTH_SHORT).show();
}
}
});
}
}

and then my xml file looks like this;

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.RatingActivity&quot;
android:orientation=&quot;horizontal&quot;&gt;
&lt;TextView
android:id=&quot;@+id/textView2&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;10dp&quot;
android:gravity=&quot;center_horizontal&quot;
android:text=&quot;We hope you enjoyed your meal with us today&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;italic&quot; /&gt;
&lt;RatingBar
android:id=&quot;@+id/ratingBar&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:numStars=&quot;5&quot;
android:stepSize=&quot;1&quot;
android:rating=&quot;5&quot;/&gt;
&lt;TextView
android:id=&quot;@+id/tvRatingScale&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center&quot;
android:layout_marginTop=&quot;10dp&quot;
android:text=&quot;Awesome. I love it&quot;
android:textSize=&quot;16sp&quot;
android:textStyle=&quot;bold&quot;/&gt;
&lt;EditText
android:id=&quot;@+id/etFeedback&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:inputType=&quot;textMultiLine&quot;
android:lines=&quot;5&quot;
android:hint=&quot;Tell us a little about why you have given us this rating&quot;
android:gravity=&quot;top&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btnSubmit&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;10dp&quot;
android:background=&quot;#e57373&quot;
android:text=&quot;Send feedback&quot;
android:textColor=&quot;@android:color/white&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;TextView&quot;
tools:layout_editor_absoluteX=&quot;17dp&quot;
tools:layout_editor_absoluteY=&quot;46dp&quot; /&gt;
&lt;/LinearLayout&gt;

The text "We hope you enjoyed your meal with us today" is all that appears on the screen when the onCreate method is called. I can't seem to find a reason why.

答案1

得分: 0

这是因为您将方向设置为水平

android:orientation="horizontal"

您需要将其更新为垂直

android:orientation="vertical"

以下是您更新后的 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RatingActivity"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="We hope you enjoyed your meal with us today"
android:textSize="18sp"
android:textStyle="italic" />
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1"
android:rating="5" />
<TextView
android:id="@+id/tvRatingScale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Awesome. I love it"
android:textSize="16sp"
android:textStyle="bold" />
<EditText
android:id="@+id/etFeedback"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="5"
android:hint="Tell us a little about why you have given us this rating"
android:gravity="top" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#e57373"
android:text="Send feedback"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
tools:layout_editor_absoluteX="17dp"
tools:layout_editor_absoluteY="46dp" />
</LinearLayout>
英文:

This is because you sets orientation horizontal

android:orientation=&quot;horizontal&quot;

you need to update it to vertical

android:orientation=&quot;vertical&quot;

Here is your updated xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.RatingActivity&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;TextView
android:id=&quot;@+id/textView2&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;10dp&quot;
android:gravity=&quot;center_horizontal&quot;
android:text=&quot;We hope you enjoyed your meal with us today&quot;
android:textSize=&quot;18sp&quot;
android:textStyle=&quot;italic&quot; /&gt;
&lt;RatingBar
android:id=&quot;@+id/ratingBar&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:numStars=&quot;5&quot;
android:stepSize=&quot;1&quot;
android:rating=&quot;5&quot;/&gt;
&lt;TextView
android:id=&quot;@+id/tvRatingScale&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center&quot;
android:layout_marginTop=&quot;10dp&quot;
android:text=&quot;Awesome. I love it&quot;
android:textSize=&quot;16sp&quot;
android:textStyle=&quot;bold&quot;/&gt;
&lt;EditText
android:id=&quot;@+id/etFeedback&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:inputType=&quot;textMultiLine&quot;
android:lines=&quot;5&quot;
android:hint=&quot;Tell us a little about why you have given us this rating&quot;
android:gravity=&quot;top&quot; /&gt;
&lt;Button
android:id=&quot;@+id/btnSubmit&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;10dp&quot;
android:background=&quot;#e57373&quot;
android:text=&quot;Send feedback&quot;
android:textColor=&quot;@android:color/white&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textView&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;TextView&quot;
tools:layout_editor_absoluteX=&quot;17dp&quot;
tools:layout_editor_absoluteY=&quot;46dp&quot; /&gt;
&lt;/LinearLayout&gt;

答案2

得分: 0

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RatingActivity"
    android:orientation="vertical">
...
</LinearLayout>
英文:
&lt;LinearLayout 
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.RatingActivity&quot;
android:orientation=&quot;vertical&quot;&gt;

...
</LinearLayout>

huangapple
  • 本文由 发表于 2020年4月3日 21:41:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/61013245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定