`

Android学习08-----Android中的基本控件(下)(3)_随笔提示文本:AutoCompleteTextView 拖动条:SeekBar和评分组件

阅读更多

 

一、随笔提示文本: AutoCompleteTextView

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" >

    <AutoCompleteTextView
        android:id="@+id/myauto"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
 

AutoCompleteTextViewActivity.java

package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class AutoCompleteTextViewActivity extends Activity {
	private static final String DATA[] = new String[] { "hello", "hello java",
			"hello xdwang", "hello world", "mldn Android" };
	private AutoCompleteTextView myauto = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, DATA); // 数据集
		this.myauto = (AutoCompleteTextView) super.findViewById(R.id.myauto); // 取得组件
		this.myauto.setAdapter(adapter); // 设置内容
	}
}
 

 

二、拖动条: SeekBar

1 、事件处理

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" >

    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
 

SeekBar01Activity.java

package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.SeekBar;
import android.widget.TextView;

public class SeekBar01Activity extends Activity {
	private SeekBar seekbar = null;
	private TextView text = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.seekbar = (SeekBar) super.findViewById(R.id.seekbar); // 取得组件
		this.text = (TextView) super.findViewById(R.id.text); // 取得组件
		this.text.setMovementMethod(ScrollingMovementMethod.getInstance()); // 文本组件的内容可以滚动
		this.seekbar
				.setOnSeekBarChangeListener(new OnSeekBarChangeListenerImpl());
	}

	private class OnSeekBarChangeListenerImpl implements
			SeekBar.OnSeekBarChangeListener {
		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			SeekBar01Activity.this.text.append("*** 开始拖动,当前值:"
					+ seekBar.getProgress() + "\n");
		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			SeekBar01Activity.this.text.append("*** 正在拖动,当前值:"
					+ seekBar.getProgress() + "\n");
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			SeekBar01Activity.this.text.append("*** 停止拖动,当前值:"
					+ seekBar.getProgress() + "\n");
		}

	}
}
 

 

1、  图片浏览

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" >

    <ImageView
        android:id="@+id/pic"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/pic_0" />

    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
 

SeekBar02Activity.java

package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;

public class SeekBar02Activity extends Activity {
	private SeekBar seekbar = null;
	private ImageView pic = null;
	private int picData[] = new int[] { R.drawable.pic_0, R.drawable.pic_1,
			R.drawable.pic_2, R.drawable.pic_3, R.drawable.pic_4,
			R.drawable.pic_5, R.drawable.pic_6, R.drawable.pic_7,
			R.drawable.pic_8, R.drawable.pic_9 };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.seekbar = (SeekBar) super.findViewById(R.id.seekbar); // 取得组件
		this.pic = (ImageView) super.findViewById(R.id.pic); // 取得组件
		this.seekbar.setMax(9); // 0 ~ 9的范围
		this.seekbar
				.setOnSeekBarChangeListener(new OnSeekBarChangeListenerImpl());
	}

	private class OnSeekBarChangeListenerImpl implements
			SeekBar.OnSeekBarChangeListener {
		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			SeekBar02Activity.this.pic
					.setImageResource(SeekBar02Activity.this.picData[seekBar
							.getProgress()]);// 设置显示图片
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
		}

	}
}
 

 

3 、改变屏幕亮度

SeekBar03Activity.java

package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.SeekBar;

public class SeekBar03Activity extends Activity {
	private SeekBar seekbar = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.seekbar = (SeekBar) super.findViewById(R.id.seekbar); // 取得组件
		this.seekbar.setMax(100);
		this.seekbar
				.setOnSeekBarChangeListener(new OnSeekBarChangeListenerImpl());
	}

	private class OnSeekBarChangeListenerImpl implements
			SeekBar.OnSeekBarChangeListener {
		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			SeekBar03Activity.this.setScreenBrightness((float) seekBar
					.getProgress() / 100);
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
		}
	}

	private void setScreenBrightness(float num) { // 0 ~ 1表示亮度
		WindowManager.LayoutParams layoutParams = super.getWindow()
				.getAttributes(); // 取得屏幕的属性
		layoutParams.screenBrightness = num; // 设置屏幕亮度
		super.getWindow().setAttributes(layoutParams); // 重新设置窗口的属性
	}
}
 

 

三、评分组件: RatingBar

1 、基本实现

RatingBar01Activity.java

package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

public class RatingBar01Activity extends Activity {
	private RatingBar ratingBarA = null;
	private TextView text = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.ratingBarA = (RatingBar) super.findViewById(R.id.ratingbarA);
		this.text = (TextView) super.findViewById(R.id.text);
		this.ratingBarA
				.setOnRatingBarChangeListener(new OnRatingBarChangeListenerImpl());
	}

	private class OnRatingBarChangeListenerImpl implements
			OnRatingBarChangeListener {
		@Override
		public void onRatingChanged(RatingBar ratingBar, float rating,
				boolean fromUser) {
			RatingBar01Activity.this.text.append("*** 当前值(Rating):"
					+ ratingBar.getRating() + ",步长:" + ratingBar.getStepSize()
					+ "\n");
		}
	}
}
 

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" >

    <RatingBar
        android:id="@+id/ratingbarA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:isIndicator="false"
        android:numStars="5"
        android:stepSize="0.5" />

    <RatingBar
        android:id="@+id/ratingbarB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:isIndicator="true"
        android:numStars="5"
        android:rating="3" />

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
 

2、  自定义图片

drawable-hdpi/ star_conf_file.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+android:id/background"
        android:drawable="@drawable/star_empty"/>

    <item
        android:id="@+android:id/secondaryProgress"
        android:drawable="@drawable/star_empty"/>

    <item
        android:id="@+android:id/progress"
        android:drawable="@drawable/star_full"/>

</layer-list>
 

Value/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<style name="myRatingBar" parent="@android:style/Widget.RatingBar">
		<item name="android:progressDrawable">
			@drawable/star_conf_file
		</item>
		<item name="android:minHeight">53dip</item>
		<item name="android:maxHeight">53dip</item>
	</style>
</resources>
 

RatingBar02Activity.java

package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

public class RatingBar02Activity extends Activity {
	private RatingBar ratingBar = null;
	private TextView text = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.ratingBar = (RatingBar) super.findViewById(R.id.ratingbar);
		this.text = (TextView) super.findViewById(R.id.text);
		this.ratingBar
				.setOnRatingBarChangeListener(new OnRatingBarChangeListenerImpl());
	}

	private class OnRatingBarChangeListenerImpl implements
			OnRatingBarChangeListener {
		@Override
		public void onRatingChanged(RatingBar ratingBar, float rating,
				boolean fromUser) {
			int num = (int) rating;
			String result = null; // 保存文字信息
			switch (num) {
			case 5:
				result = "非常满意";
				break;
			case 4:
				result = "满意";
				break;
			case 3:
				result = "还可以";
				break;
			case 2:
				result = "不满意";
				break;
			case 1:
				result = "非常不满意";
				break;
			}
			RatingBar02Activity.this.text.setText(result);
		}
	}
}

 

分享到:
评论
1 楼 Think_Quietly 2013-04-18  
Thank you!

相关推荐

Global site tag (gtag.js) - Google Analytics