当前位置:首页 > 开发 > 移动开发 > 正文

Android控件之AutoCompleteTextView、MultiAutoCompleteTextView探究

发表于: 2011-08-08   作者:chenhaodejia   来源:转载   浏览次数:
摘要:       在Android中提供了俩种智能输入框,它们是MultiAutoCompleteTextView、AutoCompleteTextView。它们的功能大致一样。下面详细介绍一下。   一、AutoCompleteTextView   1.简介       一个可编辑的文本视图显示自动完成建议当用户键入。建议列表显示在一个下拉菜单,

      在Android中提供了俩种智能输入框,它们是MultiAutoCompleteTextView、AutoCompleteTextView。它们的功能大致一样。下面详细介绍一下。
  一、AutoCompleteTextView
  1.简介
      一个可编辑的文本视图显示自动完成建议当用户键入。建议列表显示在一个下拉菜单,用户可以从中选择一项,以完成输入。建议列表是从一个数据适配器获取的数据。
  2.重要方法
      clearListSelection():清除选中的列表项
      dismissDropDown():如果存在关闭下拉菜单
      getAdapter():获取适配器
  3.实例
  (1)布局文件
    <AutoCompleteTextView android:id="@+id/edit"
                  android:layout_width="match_parent" android:layout_height="wrap_content" />
   (2)程序
    实例化适配器
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, strs);
    设置适配器
      edit.setAdapter(adapter);

二、MultiAutoCompleteTextView
  1.简介
      继承自AutoCompleteTextView,延长AutoCompleteTextView的长度,你必须要提供一个MultiAutoCompleteTextView.Tokenizer来区分不同的子串
  2.重要方法
      enoughToFilter():当文本长度超过阈值时过滤
      performValidation():代替验证整个文本,这个子类方法验证每个单独的文字标记
      setTokenizer(MultiAutoCompleteTextView.Tokenizer t):用户正在输入时,tokenizer设置将用于确定文本相关范围内
  3.实例
  (1)布局文件
    <<MultiAutoCompleteTextView android:id="@+id/edit1"
          android:layout_marginLeft="23px" android:layout_width="match_parent"
          android:layout_height="wrap_content" />   
  (2)程序
    实例化适配器
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, strs);
    设置适配器
      edit.setAdapter(adapter);
    确定范围
    edit1.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());


Android控件之Chronometer(定时器)

 Chronometer是一个简单的定时器,你可以给它一个开始时间,并以此定时,或者如果你不给它一个开始时间,它将会使用你的时间通话开始。默认情况下它会显示在当前定时器的值的形式“分:秒”或“H:MM:SS的”,或者可以使用的Set(字符串)格式的定时器值到一个任意字符串
1.重要属性
android:format:定义时间的格式如:hh:mm:ss
2.重要方法
setBase(long base):设置倒计时定时器
setFormat( String format):设置显示时间的格式。
start():开始计时
stop():停止计时
setOnChronometerTickListener( Chronometer.OnChronometerTickListener listener):当计时器改变时调用。

3.实例
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Chronometer android:id="@+id/chronometer"
        android:format="Initial format: "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingBottom="30dip"
        android:paddingTop="30dip"
        />
    <Button android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始">
        <requestFocus />
    </Button>
    <Button android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止">
    </Button>
    <Button android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="重置">
    </Button>
    <Button android:id="@+id/set_format"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="设置格式">
    </Button>
    <Button android:id="@+id/clear_format"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="清除格式">
    </Button>
</LinearLayout>

主程序
package wjq.WidgetDemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
public class ChronometerDemo extends Activity {
private Chronometer mChronometer;
/* (non-Javadoc)
  * @see android.app.Activity#onCreate(android.os.Bundle)
  */
@Override
protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.chronometerpage);
  Button button;
        mChronometer = (Chronometer) findViewById(R.id.chronometer);
        // Watch for button clicks.
        button = (Button) findViewById(R.id.start);
        button.setOnClickListener(mStartListener);
        button = (Button) findViewById(R.id.stop);
        button.setOnClickListener(mStopListener);
        button = (Button) findViewById(R.id.reset);
        button.setOnClickListener(mResetListener);
        button = (Button) findViewById(R.id.set_format);
        button.setOnClickListener(mSetFormatListener);
        button = (Button) findViewById(R.id.clear_format);
        button.setOnClickListener(mClearFormatListener);
    }
    View.OnClickListener mStartListener = new OnClickListener() {
        public void onClick(View v) {
            mChronometer.start();
        }
    };
    View.OnClickListener mStopListener = new OnClickListener() {
        public void onClick(View v) {
            mChronometer.stop();
        }
    };
    View.OnClickListener mResetListener = new OnClickListener() {
        public void onClick(View v) {
            mChronometer.setBase(SystemClock.elapsedRealtime());
        }
    };
    View.OnClickListener mSetFormatListener = new OnClickListener() {
        public void onClick(View v) {
            mChronometer.setFormat("Formatted time (%s)");
        }
    };
    View.OnClickListener mClearFormatListener = new OnClickListener() {
        public void onClick(View v) {
            mChronometer.setFormat(null);
        }
    };
}


Android 控件之DatePicker,TimePicker,Calender
 在Android中关于日期时间的类有TimePicker、DatePicker、TimePickerDialog、DatePickerDialog、Calendar。其中TimePickerDialog、DatePickerDialog是对话框形式。
一、TimePicker
  查看一个在24小时或上午/下午模式下一天的时间。
  1.重要方法
    setCurrentMinute(Integer currentMinute)设置当前时间的分钟
    getCurrentMinute()获取当前时间的分钟
    setEnabled(boolean enabled)设置当前视图是否可以编辑。
    setOnTimeChangedListener(TimePicker.OnTimeChangedListener onTimeChangedListener)当时间改变时调用
  2.实例:
timePicker=(TimePicker)findViewById(R.id.timePicker);
  timePicker.setCurrentHour(16);
  timePicker.setCurrentMinute(10);
  updateDisplay(16,10);
  timePicker.setOnTimeChangedListener(this);

二、DatePicker
    1.重要方法
      getDayOfMonth():获取当前Day
      getMonth():获取当前月
      getYear()获取当前年
      updateDate(int year, int monthOfYear, int dayOfMonth):更新日期
三、TimePickerDialog、DatePickerDialog
  以对话框形式显示日期时间视图
四、Calendar
    日历是设定年度日期对象和一个整数字段之间转换的抽象基类,如,月,日,小时等。
  实例
final Calendar calendar=Calendar.getInstance();
  mYear=calendar.get(Calendar.YEAR);
  mMonth=calendar.get(Calendar.MONTH);
  mDay=calendar.get(Calendar.DAY_OF_MONTH);
  mHour=calendar.get(Calendar.HOUR_OF_DAY);
  mMinute=calendar.get(Calendar.MINUTE);
此类方法不做赘述



Android 控件之Gallery图片集

一、简介
  在中心锁定,水平显示列表的项。
二、实例
1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
   android:orientation="vertical"
  android:layout_height="wrap_content">


  <Gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Gallery android:id="@+id/gallery1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:spacing="16dp"
    />
</LinearLayout>

</LinearLayout>

2.属性文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TogglePrefAttrs">
        <attr name="android:preferenceLayoutChild" />
    </declare-styleable>


    <!-- These are the attributes that we want to retrieve from the theme
         in view/Gallery1.java -->
    <declare-styleable name="Gallery1">
        <attr name="android:galleryItemBackground" />
    </declare-styleable>


     <declare-styleable name="LabelView">
        <attr name="text" format="string" />
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />
    </declare-styleable>
</resources>

3.代码
/**
*
*/
package wjq.WidgetDemo;
import android.R.layout;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.SpinnerAdapter;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
/**
* @author 记忆的永恒
*
*/
public class GalleryDemo extends Activity {
private Gallery gallery;
private Gallery gallery1;
/*
  * (non-Javadoc)
  *
  * @see android.app.Activity#onCreate(android.os.Bundle)
  */
@Override
protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.gallerypage);
  gallery = (Gallery) findViewById(R.id.gallery);
  gallery.setAdapter(new ImageAdapter(this));


  registerForContextMenu(gallery);


   Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
         startManagingCursor(c);


         SpinnerAdapter adapter = new SimpleCursorAdapter(this,
         // Use a template that displays a text view
                 android.R.layout.simple_gallery_item,
                 // Give the cursor to the list adatper
                 c,
                 // Map the NAME column in the people database to...
                 new String[] {People.NAME},
                 // The "text1" view defined in the XML template
                 new int[] { android.R.id.text1 });
         gallery1= (Gallery) findViewById(R.id.gallery1);
         gallery1.setAdapter(adapter);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
   ContextMenuInfo menuInfo) {
  menu.add("Action");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
  AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
    .getMenuInfo();
  Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT)
    .show();
  return true;
}


public class ImageAdapter extends BaseAdapter {
  int mGalleryItemBackground;
  private Context mContext;
  private Integer[] mImageIds = { R.drawable.b, R.drawable.c,
    R.drawable.d, R.drawable.f, R.drawable.g };
  public ImageAdapter(Context context) {
   mContext = context;
   TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
   mGalleryItemBackground = a.getResourceId(
     R.styleable.Gallery1_android_galleryItemBackground, 0);
   a.recycle();
  }
  @Override
  public int getCount() {
   return mImageIds.length;
  }
  @Override
  public Object getItem(int position) {
   return position;
  }
  @Override
  public long getItemId(int position) {
   return position;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   ImageView i = new ImageView(mContext);
   i.setImageResource(mImageIds[position]);
   i.setScaleType(ImageView.ScaleType.FIT_XY);
   i.setLayoutParams(new Gallery.LayoutParams(300, 400));
   // The preferred Gallery item background
   i.setBackgroundResource(mGalleryItemBackground);
   return i;
  }
}

}




Android 控件之ImageSwitcher图片切换器

一、重要方法
     setImageURI( Uri uri):设置图片地址
     setImageResource(int resid):设置图片资源库
     setImageDrawable( Drawable drawable):绘制图片
二、实例
   <ImageSwitcher android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
    />
   
    <Gallery android:id="@+id/gallery"
        android:background="#55000000"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
       
        android:gravity="center_vertical"
        android:spacing="16dp"
    />

is = (ImageSwitcher) findViewById(R.id.switcher);
is.setFactory(this);

设置动画效果
  is.setInAnimation(AnimationUtils.loadAnimation(this,
    android.R.anim.fade_in));
  is.setOutAnimation(AnimationUtils.loadAnimation(this,
    android.R.anim.fade_out));
  
三、完整代码
1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageSwitcher android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
    />


    <Gallery android:id="@+id/gallery"
        android:background="#55000000"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"


        android:gravity="center_vertical"
        android:spacing="16dp"
    />
</RelativeLayout>
   2.Java代码
package wjq.WidgetDemo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;
public class ImageSwitcherDemo extends Activity implements
  OnItemSelectedListener, ViewFactory {
private ImageSwitcher is;
private Gallery gallery;
private Integer[] mThumbIds = { R.drawable.b, R.drawable.c,
   R.drawable.d, R.drawable.f, R.drawable.g,
   };
private Integer[] mImageIds = { R.drawable.b, R.drawable.c,
   R.drawable.d, R.drawable.f, R.drawable.g, };
/*
  * (non-Javadoc)
  *
  * @see android.app.Activity#onCreate(android.os.Bundle)
  */
@Override
protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.imageswitcherpage);
  is = (ImageSwitcher) findViewById(R.id.switcher);
  is.setFactory(this);
  is.setInAnimation(AnimationUtils.loadAnimation(this,
    android.R.anim.fade_in));
  is.setOutAnimation(AnimationUtils.loadAnimation(this,
    android.R.anim.fade_out));
  gallery = (Gallery) findViewById(R.id.gallery);
  gallery.setAdapter(new ImageAdapter(this));
  gallery.setOnItemSelectedListener(this);
}
@Override
public View makeView() {
  ImageView i = new ImageView(this);
  i.setBackgroundColor(0xFF000000);
  i.setScaleType(ImageView.ScaleType.FIT_CENTER);
  i.setLayoutParams(new ImageSwitcher.LayoutParams(
    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
  return i;
}
public class ImageAdapter extends BaseAdapter {
  public ImageAdapter(Context c) {
   mContext = c;
  }
  public int getCount() {
   return mThumbIds.length;
  }
  public Object getItem(int position) {
   return position;
  }
  public long getItemId(int position) {
   return position;
  }
  public View getView(int position, View convertView, ViewGroup parent) {
   ImageView i = new ImageView(mContext);
   i.setImageResource(mThumbIds[position]);
   i.setAdjustViewBounds(true);
   i.setLayoutParams(new Gallery.LayoutParams(
     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
   i.setBackgroundResource(R.drawable.e);
   return i;
  }
  private Context mContext;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
   long id) {
  is.setImageResource(mImageIds[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
  // TODO Auto-generated method stub
}
}



Android 控件之ProgressBar进度条

下面详细介绍ProgressBar
一、说明
  在某些操作的进度中的可视指示器,为用户呈现操作的进度,还它有一个次要的进度条,用来显示中间进度,如在流媒体播放的缓冲区的进度。一个进度条也可不确定其进度。在不确定模式下,进度条显示循环动画。这种模式常用于应用程序使用任务的长度是未知的。
二、XML重要属性
    android:progressBarStyle:默认进度条样式
    android:progressBarStyleHorizontal:水平样式


三、重要方法
    getMax():返回这个进度条的范围的上限
    getProgress():返回进度
    getSecondaryProgress():返回次要进度
    incrementProgressBy(int diff):指定增加的进度
    isIndeterminate():指示进度条是否在不确定模式下
    setIndeterminate(boolean indeterminate):设置不确定模式下
    setVisibility(int v):设置该进度条是否可视
四、重要事件
    onSizeChanged(int w, int h, int oldw, int oldh):当进度值改变时引发此事件
五、实例
1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ProgressBar android:id="@+id/progress_horizontal"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="75" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="默认进度条" />       
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button android:id="@+id/decrease"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="减少" />
        <Button android:id="@+id/increase"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="增加" />
    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义进度条" />       
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button android:id="@+id/decrease_secondary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第二减少" />
        <Button android:id="@+id/increase_secondary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第二增加" />
    </LinearLayout>
</LinearLayout>

2.Java代码
package wjq.WidgetDemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
public class ProgressBarDemo extends Activity {
/* (non-Javadoc)
  * @see android.app.Activity#onCreate(android.os.Bundle)
  */
@Override
protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);


   requestWindowFeature(Window.FEATURE_PROGRESS);
         setContentView(R.layout.probarpage);
         setProgressBarVisibility(true);


         final ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);
         setProgress(progressHorizontal.getProgress() * 100);
         setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);


         Button button = (Button) findViewById(R.id.increase);
         button.setOnClickListener(new Button.OnClickListener() {
             public void onClick(View v) {
                 progressHorizontal.incrementProgressBy(1);
                 // Title progress is in range 0..10000
                 setProgress(100 * progressHorizontal.getProgress());
             }
         });
         button = (Button) findViewById(R.id.decrease);
         button.setOnClickListener(new Button.OnClickListener() {
             public void onClick(View v) {
                 progressHorizontal.incrementProgressBy(-1);
                 // Title progress is in range 0..10000
                 setProgress(100 * progressHorizontal.getProgress());
             }
         });
         button = (Button) findViewById(R.id.increase_secondary);
         button.setOnClickListener(new Button.OnClickListener() {
             public void onClick(View v) {
                 progressHorizontal.incrementSecondaryProgressBy(1);
                 // Title progress is in range 0..10000
                 setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
             }
         });
         button = (Button) findViewById(R.id.decrease_secondary);
         button.setOnClickListener(new Button.OnClickListener() {
             public void onClick(View v) {
                 progressHorizontal.incrementSecondaryProgressBy(-1);
                 // Title progress is in range 0..10000
                 setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
             }
         });


}
}


Android 控件之RatingBar评分条

一、概述
    RatingBar是SeekBar和ProgressBar的扩展,用星星来评级。使用的默认大小RatingBar时,用户可以触摸/拖动或使用键来设置评分,它有俩种样式(大、小),其中大的只适合指示,不适合于用户交互。
二、实例
1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:paddingLeft="10dip"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RatingBar android:id="@+id/ratingbar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="3"
        android:rating="2.5" />
    <RatingBar android:id="@+id/ratingbar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:rating="2.25" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip">


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


        <RatingBar android:id="@+id/small_ratingbar"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_marginLeft="5dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical" />


    </LinearLayout>
    <RatingBar android:id="@+id/indicator_ratingbar"
        style="?android:attr/ratingBarStyleIndicator"
        android:layout_marginLeft="5dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />


</LinearLayout>
2.Java代码
package wjq.WidgetDemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.RatingBar.OnRatingBarChangeListener;
public class RatingBarDemo extends Activity implements
  OnRatingBarChangeListener {
private RatingBar mSmallRatingBar;
private RatingBar mIndicatorRatingBar;
private TextView mRatingText;
/*
  * (non-Javadoc)
  *
  * @see android.app.Activity#onCreate(android.os.Bundle)
  */
@Override
protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.ratingbarpage);


   mRatingText = (TextView) findViewById(R.id.rating);
         // We copy the most recently changed rating on to these indicator-only
         // rating bars
         mIndicatorRatingBar = (RatingBar) findViewById(R.id.indicator_ratingbar);
         mSmallRatingBar = (RatingBar) findViewById(R.id.small_ratingbar);


         // The different rating bars in the layout. Assign the listener to us.
         ((RatingBar)findViewById(R.id.ratingbar1)).setOnRatingBarChangeListener(this);
         ((RatingBar)findViewById(R.id.ratingbar2)).setOnRatingBarChangeListener(this);
}
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
   boolean fromUser) {
   final int numStars = ratingBar.getNumStars();
         mRatingText.setText(
                  " 受欢迎度" + rating + "/" + numStars);
         // Since this rating bar is updated to reflect any of the other rating
         // bars, we should update it to the current values.
         if (mIndicatorRatingBar.getNumStars() != numStars) {
             mIndicatorRatingBar.setNumStars(numStars);
             mSmallRatingBar.setNumStars(numStars);
         }
         if (mIndicatorRatingBar.getRating() != rating) {
             mIndicatorRatingBar.setRating(rating);
             mSmallRatingBar.setRating(rating);
         }
         final float ratingBarStepSize = ratingBar.getStepSize();
         if (mIndicatorRatingBar.getStepSize() != ratingBarStepSize) {
             mIndicatorRatingBar.setStepSize(ratingBarStepSize);
             mSmallRatingBar.setStepSize(ratingBarStepSize);
         }
}
}


Android 控件之Spinner

一、概述
    Spinner是一个每次只能选择所有项的一个项的控件。它的项来自于与之相关联的适配器中。
二、重要属性
    android:prompt:当Spinner对话框关闭时显示该提示
三、重要方法
    setPrompt(CharSequence prompt):设置当Spinner对话框关闭时显示的提示
    performClick():如果它被定义就调用此视图的OnClickListener
    setOnItemClickListener(AdapterView.OnItemClickListener l):当项被点击时调用
    onDetachedFromWindow():当Spinner脱离窗口时被调用。
四、完整代码
public class SpinnerDemo extends Activity {
   @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.spinnerpage);
         Spinner s1 = (Spinner) findViewById(R.id.spinnercolor);
         ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                 this, R.array.colors, android.R.layout.simple_spinner_item);
         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         s1.setAdapter(adapter);
         s1.setOnItemSelectedListener(
                 new OnItemSelectedListener() {
                     public void onItemSelected(
                             AdapterView<?> parent, View view, int position, long id) {
                         showToast("Spinner1: position=" + position + " id=" + id);
                     }
                     public void onNothingSelected(AdapterView<?> parent) {
                         showToast("Spinner1: unselected");
                     }
                 });
         Spinner s2 = (Spinner) findViewById(R.id.spinnerplanet);
         adapter = ArrayAdapter.createFromResource(this, R.array.planets,
                 android.R.layout.simple_spinner_item);
         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         s2.setAdapter(adapter);
         s2.setOnItemSelectedListener(
                 new OnItemSelectedListener() {
                     public void onItemSelected(
                             AdapterView<?> parent, View view, int position, long id) {
                         showToast("Spinner2: position=" + position+1 + " id=" + id+1);
                     }
                     public void onNothingSelected(AdapterView<?> parent) {
                         showToast("Spinner2: unselected");
                     }
                 });
     }


private void showToast(CharSequence msg) {
         Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
     }
}





Android控件之AutoCompleteTextView、MultiAutoCompleteTextView探究

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
在Android中提供了两种智能输入框,它们是AutoCompleteTextView、MultiAutoCompleteTextView。它们的
在Android中提供了两种智能输入框,它们是AutoCompleteTextView、MultiAutoCompleteTextView。它们
概述 在html中有很多自动完成的应用,例如,百度的搜索,输入一些关键字,下面就会自动列出该关键字
AutoCompleteTextView: public class Main extends Activity { private AutoCompleteTextView auto
这一章我们来介绍AutoCompleteTextView和MultiAutoCompleteTextView控件,这两个控件都是EditText的
1、简介 在我们平常上网的时候经常会用到谷歌或百度,在输入框中输入我们想要输入的信息就会出现其
在android中,文本控件主要包括TextView控件和EditView控件,本节先对TextView控件的用法进行详细介
EditView类继承自TextView类,EditView与TextView最大的不同就是用户可以对EditView控件进行编辑,同
在android中,文本控件主要包括TextView控件和EditView控件,本节先对TextView控件的用法进行详细介
EditView类继承自TextView类,EditView与TextView最大的不同就是用户可以对EditView控件进行编辑,
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号