评级栏未出现
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("Awful!");
star1++;
break;
case 2:
mRatingScale.setText("Not great...");
star2++;
break;
case 3:
mRatingScale.setText("Good");
star3++;
break;
case 4:
mRatingScale.setText("Great");
star4++;
break;
case 5:
mRatingScale.setText("Fantastic!");
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, "Please fill in feedback text box",
Toast.LENGTH_LONG).show();
}
else{
mFeedback.setText("");
mRatingBar.setRating(0);
Toast.makeText(RatingActivity.this, "Thank you for sharing your feedback :) ",
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="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>
文本"We hope you enjoyed your meal with us today"是调用onCreate方法时出现在屏幕上的全部内容。我似乎找不到原因。
这是因为您将方向设置为水平
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>
<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">
...
我正在尝试在 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("Awful!");
star1++;
break;
case 2:
mRatingScale.setText("Not great...");
star2++;
break;
case 3:
mRatingScale.setText("Good");
star3++;
break;
case 4:
mRatingScale.setText("Great");
star4++;
break;
case 5:
mRatingScale.setText("Fantastic!");
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, "Please fill in feedback text box",
Toast.LENGTH_LONG).show();
}
else{
mFeedback.setText("");
mRatingBar.setRating(0);
Toast.makeText(RatingActivity.this, "Thank you for sharing your feedback :) ",
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="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>
文本"We hope you enjoyed your meal with us today"是调用onCreate方法时出现在屏幕上的全部内容。我似乎找不到原因。
这是因为您将方向设置为水平
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>
<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">
...