英文:
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 = "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();
}
}
});
}
}
and then my xml file looks like this;
<?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>
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="horizontal"
you need to update it to vertical
android:orientation="vertical"
Here is your updated 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>
答案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>
英文:
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论