Android |创建分级栏

RatingBar允许用户对某些产品进行评分。在下面的代码中 getRating() 函数用于计算产品的评级。这个 getRating() 函数返回双类型值。

null

在Android中创建分级栏涉及以下步骤:

  1. 创建一个新的android项目。
  2. 在活动主菜单中添加分级栏。xml。
  3. 添加按钮以调用操作。
  4. 使用文本视图显示评分。
  • 要在应用程序中使用评级栏,我们将使用内置的评级栏小部件,因此第一步是将其导入到项目中。
  • 在main活动中,创建由变量“rt”表示的RatingBar对象,并在XML文件中找到相应的视图。这是通过findViewById()方法完成的。java对象成功绑定到其视图后,创建用户将与之交互的“stars”布局,以设置评级。
  • 要获取可绘制的星星,使用方法rt.getProcessDrawable()。然后使用setColorFilter()方法和参数Color来修改星星的颜色。黄色通过了。最后,编写Call方法以通过rt.getMethod()方法提取用户选择的评级值。

创建MainActivity的程序:

// Below is the code for MainActivity.java
package com.example.hp.rating;
// importing required libraries
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
RatingBar rt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//binding MainActivity.java with activity_main.xml file
rt = (RatingBar) findViewById(R.id.ratingBar);
//finding the specific RatingBar with its unique ID
LayerDrawable stars=(LayerDrawable)rt.getProgressDrawable();
//Use for changing the color of RatingBar
stars.getDrawable( 2 ).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
}
public void Call(View v)
{
// This function is called when button is clicked.
// Display ratings, which is required to be converted into string first.
TextView t = (TextView)findViewById(R.id.textView2);
t.setText( "You Rated :" +String.valueOf(rt.getRating()));
}
}


注: 对于布局,如果你是初学者,ConstraintLayout很适合使用,因为它可以根据屏幕调整视图。 这个XML文件定义了应用程序的视图。

为MainActivity创建布局的程序:

<? xml version = "1.0" encoding = "utf-8" ?>
< android.support.constraint.ConstraintLayout
<!-- Cover the entire width of the screen -->
android:layout_width="match_parent"
<!-- Cover the entire height of the screen -->
android:layout_height="match_parent"
tools:context="com.example.hp.rating.MainActivity"
android:background="@color/colorPrimary">
< RatingBar
android:id = "@+id/ratingBar"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_marginTop = "104dp"
android:background = "@color/colorPrimary"
app:layout_constraintLeft_toLeftOf = "parent"
app:layout_constraintRight_toRightOf = "parent"
app:layout_constraintTop_toTopOf = "parent"
tools:layout_constraintLeft_creator = "1"
tools:layout_constraintRight_creator = "1"
tools:layout_constraintTop_creator = "1" />
< TextView
android:id = "@+id/textView"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Rate Me!!!"
android:textColor = "@android:color/background_dark"
android:textSize = "30sp"
android:textStyle = "bold|italic"
tools:layout_editor_absoluteX = "127dp"
tools:layout_editor_absoluteY = "28dp" />
< TextView
android:id = "@+id/textView2"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_marginTop = "148dp"
android:textColorHint = "@color/colorAccent"
android:textSize = "24sp"
app:layout_constraintLeft_toLeftOf = "parent"
app:layout_constraintRight_toRightOf = "parent"
app:layout_constraintTop_toBottomOf = "@+id/ratingBar"
tools:layout_constraintRight_creator = "1"
tools:layout_constraintLeft_creator = "1" />
< Button
android:id = "@+id/button"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_marginBottom = "50dp"
android:layout_marginTop = "50dp"
android:background = "@android:color/holo_red_dark"
android:onClick = "Call"
android:text = "Submit"
android:textColor = "@android:color/background_light"
android:textStyle = "bold|italic"
app:layout_constraintBottom_toTopOf = "@+id/textView2"
app:layout_constraintLeft_toLeftOf = "parent"
app:layout_constraintRight_toRightOf = "parent"
app:layout_constraintTop_toBottomOf = "@+id/ratingBar"
tools:layout_constraintBottom_creator = "1"
tools:layout_constraintLeft_creator = "1"
tools:layout_constraintRight_creator = "1"
tools:layout_constraintTop_creator = "1" />
</ android.support.constraint.ConstraintLayout >


在这里,我们不需要更改清单文件,不需要对 分级杆 。默认情况下,清单文件中会提到所有创建的新活动。

以下是AndroidManifest的代码。xml

<? xml version = "1.0" encoding = "utf-8" ?>
< manifest xmlns:android = " http://schemas.android.com/apk/res/android "
package = "com.example.hp.rating" >
< application
android:allowBackup = "true"
android:icon = "@mipmap/ic_launcher"
android:label = "@string/app_name"
android:roundIcon = "@mipmap/ic_launcher_round"
android:supportsRtl = "true"
android:theme = "@style/AppTheme" >
< activity android:name = ".MainActivity" >
< intent-filter >
< action android:name = "android.intent.action.MAIN" />
< category android:name = "android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
</ application >
</ manifest >


输出: 图片[1]-Android |创建分级栏-yiteyi-C++库

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享