ウェブアプリケーション,インジェクション,コマンドインジェクション

Androidアプリ開発 Viewをつかったインターフェースデザイン

Viewの部品 Star rating systemを使ってみる

Implementing a Star Rating System

チェックボックスの部分では、starチェックボックスに特定して
紹介しましたが、starチェックボックスを使う一般的なものの1つは、
star rating systemを実行し、stars上のユーザのタップを
特定のアイテムを評価に供給します。
以下のコードでこの評価システムをどのように実行するかを示します。
以下のようなコードがactivity_main.xmlファイルにあるとします。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="horizontal"
   android:id="@+id/ratings" />

   <CheckBox
     android:id="@+id/star1"
     style="?android:attr.starStyle"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:tag="1" />

   <CheckBox
     android:id="@+id/star2"
     style="?android:attr/starStyle"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:tag="2" />

   <CheckBox
     android:id="@+id/star3"
     style="?android:attr/starStyle"
     android:layout_width="wrap_content"
     android:layour_height="wrap_content"
     android:tag="3" />

   <CheckBox
     android:id="@+id/star4"
     style="?android:attr/starStyle"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:tag="4" />

   <CheckBox
     android:id="@+id/star5"
     style="?android:attr/starStyle"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:tag="5" />:
<LinearLayout>

チェックボックスは横に整えられ、starスタイルを使って
表示されることに注意します。
また、それぞれのチェックボックスはandroid:tag属性を使って
印をつけます。
これは、個々のチェックボックスをプログラムに従って
配置する必要が出てきたときに後で役に立つでしょう
以下のコード部はstar rating systemをどのように実行するかを
示します。

package net.learn2develop.checkboxesratings;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Checkbox;
import android.widget.LinearLayout;

public class MainActivity extends Activity{
  LinearLayout rating;
  CheckBox star;

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

    //---get the layout containing the stars---
    rating = (LinearLayout) findViewById(R.id.ratings);

    for (int i = 1; i <= 5; i++){
      star = (CheckBox) rating.findViewWithTag(String.valueOf(i));
      star.setOnClickListener(starsListener);
   }
  }

  private OnClickListener starsListener = new OnClickListener(){
   public void onClick(View view){

    //---get the tag of the star selected---
    int tag = Integet.valueOf((String) view.getTag());

    //---check all the stars up to the touched---
    for (int i = 1; i <= tag; i++){
      star = (CheckBox) rating.findViewWithTag(String.valueOf(i));
      star.setChecked(true);
    }

    //---uncheck all remaining stars---
    for (int i = tag + 1; i <= 5; i++){
      star = (CheckBox) rating.findViewWithTag(String.valueOf(i));
      star.setChecked(false);
    }
   }
  };
}

5つすべてのチェックボックスをまず配置して、
それぞれのチェックボックスのonClick()イベントに同調させます。
上のコードではチェックボックスのすべてがLinearLayoutの中に
組み込まれているので、
LinearLayoutインスタンスのfindViewWithTag()メソッドを使って
個々のチェックボックスを配置します。
チェックボックスがタップされた時には、
チェックボックスがタップされて、そしてその後、
すべてのチェックボックスからチェックが外されるまで、
チェックボックスをチェックします。

ホーム
便利堂ロゴ
inserted by FC2 system