【问题标题】:How to create Rate button?如何创建速率按钮?
【发布时间】:2014-04-14 06:53:52
【问题描述】:

我几乎完成了第一款 Android 游戏(混合应用),我正在考虑为它创建一个评分按钮。我尝试在互联网上搜索,但大多数 cmets 都在谈论 Java 代码,而我什么都不知道。假设我在 HTML 文件 ( Rate ) 中创建了一个按钮。每个人都可以给出一个解决方案,让它作为一个常规的 Rate 按钮运行?

【问题讨论】:

标签: android


【解决方案1】:

试试这个:

打开“res/layout/main.xml”文件,添加一个评分栏组件,几个文本视图和一个按钮。

文件:res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/lblRateMe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Rate Me"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="4"
        android:stepSize="1.0"
        android:rating="2.0" />

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/lblResult"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Result : "
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/txtRatingValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </LinearLayout>

</LinearLayout>

2。活动代码

在活动“onCreate()”方法中,在评分栏上附加一个监听器,当评分值改变时触发。按钮上的另一个侦听器,单击按钮时触发。阅读代码的注释,它应该是不言自明的。

文件:MyAndroidAppActivity.java

package com.xyz.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MyAndroidAppActivity extends Activity {

  private RatingBar ratingBar;
  private TextView txtRatingValue;
  private Button btnSubmit;

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

    addListenerOnRatingBar();
    addListenerOnButton();

  }

  public void addListenerOnRatingBar() {

    ratingBar = (RatingBar) findViewById(R.id.ratingBar);
    txtRatingValue = (TextView) findViewById(R.id.txtRatingValue);

    //if rating value is changed,
    //display the current rating value in the result (textview) automatically
    ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
        public void onRatingChanged(RatingBar ratingBar, float rating,
            boolean fromUser) {

            txtRatingValue.setText(String.valueOf(rating));

        }
    });
  }

  public void addListenerOnButton() {

    ratingBar = (RatingBar) findViewById(R.id.ratingBar);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);

    //if click on me, then display the current rating value.
    btnSubmit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(MyAndroidAppActivity.this,
                String.valueOf(ratingBar.getRating()),
                    Toast.LENGTH_SHORT).show();

        }

    });

  }
}

【讨论】:

  • Eureka,一切都比我想象的要容易,因为我有一个基于 html 的应用程序,我可以简单地在 Google Play 上添加一个指向我的应用程序页面的链接。 :) ps:不管怎样,非常感谢!!!
【解决方案2】:

这是您游戏的解决方案。您可以使用它 - https://github.com/kskkbys/Android-RateThisApp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    相关资源
    最近更新 更多