`

Android美团评分组件及消息提示框自我定制(14)

阅读更多

评分组件也疯狂

 

美团完,淘宝完,消费完,那些各种评分组件是不是让你也疯狂呢,那么,现在我们自己来做一个评分组件,小星星登场。

RatingBar 这里我们定义两个五角星评分组件,以便显示我们定义与系统默认的区别。

首先布局文档:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/TableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RatingBar
            android:id="@+id/ratingBar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:textSize="50px"
            android:layout_height="fill_parent" />

    </TableRow>

    <RatingBar
        android:id="@+id/ratingBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="4"//星星的数目为4颗
        android:rating="3" />//评分为三颗星

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="评分完成
        " />

</TableLayout>

 

 

 

这里我们用TableRow布局,而且这一节课我们会讲到Toast信息提示框的使用,就是我们经常在手机点了什么,下面出现的一个信息框,然后一会又隐去的那个。MainActivity.java文件代码:

 

rivate RatingBar rating=null;
	private TextView text=null;
	private Button bt;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.rating=(RatingBar)super.findViewById(R.id.ratingBar1);
		this.text=(TextView)super.findViewById(R.id.textView1);
		this.rating.setOnRatingBarChangeListener(new Rating());
		this.bt=(Button)super.findViewById(R.id.button1);
		this.bt.setOnClickListener(new click());
	}
	相信经过前面几节课的学习,大家对上一段代码都很熟悉了,下面就是事件处理了。这里我们模拟一下美团的评分系统,之前做的美团美食列表评分只是图片,现在我们真枪实战:
private class Rating implements OnRatingBarChangeListener{

		@Override
		public void onRatingChanged(RatingBar ratingbar, float rating, boolean fromuser) {
			// TODO Auto-generated method stub
			//text.append("当前评分:"+ratingbar.getRating()+"   "+"增长步长: "+ratingbar.getStepSize());
			String result=null;
			switch((int)rating){
			case 5:
				result="非常满意";
				break;
			case 4:
				result="满意";
				break;
			case 3:
				result="还可以";
				break;
			case 2:
				result="不满意";
				break;
			case 1:
				result="非常不满意";
				break;
		
			}
			text.setText(result);
		}
		
	}

 

 

 

然后处理button事件,这里我们实现一个与系统默认不一样的信息提示框,带图片自定义的:

 

<!--EndFragment-->

 

private class click implements OnClickListener{

		@Override
		public void onClick(View arg0) {
			Toast t=Toast.makeText(MainActivity.this, "长时间显示", Toast.LENGTH_LONG);//这里可以定义长时间和短时间显示
			t.setGravity(Gravity.CENTER, 60, 30);//对齐方式及位置
			//取得toast的view组件,以便添加图片
			LinearLayout myview=(LinearLayout)t.getView();
			ImageView image=new ImageView(MainActivity.this);
			image.setImageResource(R.drawable.back);//设置图片资源
			myview.addView(image,0);
			t.show();
			
			
		}
		
	}

 

注意一定要得到Toastview组件才可以添加图片

 

实现效果如下:



 

 

<!--EndFragment--><!--EndFragment--><!--EndFragment-->

  • 大小: 213.3 KB
  • 大小: 90 KB
  • 大小: 154.9 KB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics