一、自动完成文本框AutoCompleteTextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:completionHint="请选择您喜欢的图书"
        android:dropDownHorizontalOffset="20dp"
        android:completionThreshold="3"/>
   
//定义字符串数组,作为提示的文本
 String[] books = new String[]{
  "会有天使替我爱你",
  "梦里花落知多少",
  "幻城",
  "猜火车",
  "三重门",
  "android疯狂讲义"
 };
//创建一个ArrayAdapter,封装数组
        ArrayAdapter aa = new ArrayAdapter(
          this, android.R.layout.simple_dropdown_item_1line,books);
        AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.auto);
        //设置Adapter
        actv.setAdapter(aa);
二、Spinner列表选择框
1.Spinner组件与Swing编程中的Spinner不同,此处的Spinner其实就是一个列表选择框,不过Android
的列表选择框并不是需要显示下拉列表的,而是相当于弹出一个菜单供用户选择

          android:layout_height="wrap_content"
       android:entries="@array/books"/>
  
arrays.xml放在values目录下

   
        会有天使替我爱你
        梦里花落知多少
        三重门
   


2.不存储列表项的Spinner
对于Spinner而言,它也许需要显示一个用户选择的列表---至于这个列表
到底以数组的形式给出,还是以其它的什么形式给出,这不重要。
重要的是Spinner知道它的每项应该显示什么

       android:layout_width="fill_parent"
       android:layout_height="wrap_content"/>
BaseAdapter ba = new BaseAdapter() {
         //重写该方法,该方法返回的View将作为列表框的每项
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
    TextView text = new TextView(SpinnerTest2Activity.this);
    text.setText(position + "");
    text.setTextSize(20);
    text.setTextColor(R.color.red);
    return text;
   }
   
   @Override
   public int getCount() {
    //指定一共包含10个选项
    return 10;
   }
   
   @Override
   public long getItemId(int position) {
    return 0;
   }
   
   @Override
   public Object getItem(int position) {
    return null;
   }
  };
  Spinner spinner = (Spinner) findViewById(R.id.test);
  spinner.setAdapter(ba);
colors.xml放在values目录下

    #ff0000

通过为Spinner提供Adapter可以非常灵活的定制Spinner,包括重绘Spinner对象的列表的外观。
只要喜欢,可以让重写getView方法返回任意的View对象,该对象将会作为Spinner的列表项
修改上面的getView方法:
 //重写该方法,该方法返回的View将作为列表框的每项
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
    LinearLayout line = new LinearLayout(SpinnerTest2Activity.this);
    line.setOrientation(0);
    ImageView image = new ImageView(SpinnerTest2Activity.this);
    image.setImageResource(R.drawable.ic_launcher);
    TextView text = new TextView(SpinnerTest2Activity.this);
    text.setText(position + "");
    text.setTextSize(20);
    text.setTextColor(R.color.red);
    line.addView(image);
    line.addView(text);
    return line;
   }

三、日期、时间选择器DatePicker和TimePicker
          android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="选择购买本书的具体时间" />
   
   
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>
   
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>
   
            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:editable="false"
        android:cursorVisible="false"/>
       
DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker);
        TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker);
        //获取当前的年、月、日、小时、分钟
        Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        hour = c.get(Calendar.HOUR);
        minute = c.get(Calendar.MINUTE);
        //初始化DatePicker组件,初始化时指定监听器
        datePicker.init(year, month, day, new OnDateChangedListener() {
   
   @Override
   public void onDateChanged(DatePicker view, int year, int month,
     int day) {
    ChooseDateActivity.this.year = year;
    ChooseDateActivity.this.month = month;
    ChooseDateActivity.this.day = day;
    //显示当前日期、时间
    showDate(year, month, day, hour, minute);  
   }
  });
        timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
   @Override
   public void onTimeChanged(TimePicker view, int hour, int minute) {
    ChooseDateActivity.this.hour = hour;
    ChooseDateActivity.this.minute = minute;
    showDate(year, month, day, hour, minute);  
    
   }
  });
    }
    //定义在EditText中显示当前日期、时间的方法
    private void showDate(int year,int month,int day,int hour,int minute)
    {
     EditText show = (EditText) findViewById(R.id.show);
     show.setText("您的购买日期为:"+year + "年" +(month + 1) + "月"
       + day + "日" + hour + "时" + minute + "分");
    }

四、进度条ProgressBar
1.         android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="任务完成的进度" />
   
            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        style="@android:style/Widget.ProgressBar.Horizontal"/>
   
            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progressDrawable="@drawable/my_bar"
        style="@android:style/Widget.ProgressBar.Horizontal"/>
my_bar.xml

   
            android:drawable="@drawable/no">
   
            android:drawable="@drawable/ok">


//该程序模拟填充长度为100的数组
 private int[] data = new int[100];
 int hasData = 0;
 //记录ProgressBar的完成进度
 int statu = 0;
 
 final ProgressBar bar = (ProgressBar) findViewById(R.id.bar);
        final ProgressBar bar2 = (ProgressBar) findViewById(R.id.bar2);
        //创建一个负责更新的进度的Handler
        final Handler mHandler = new Handler()
        {
         @Override
         public void handleMessage(Message msg) {
          // 表明消息是由该程序发送的
          if(msg.what==0x111)
          {
           bar.setProgress(statu);
           bar2.setProgress(statu);
          }
          super.handleMessage(msg);
         }
        };
        //启动线程来执行任务
        new Thread()
        {
         public void run() {
          while(statu < 100)
          {
           //获取耗时操作的完成百分比
           statu = doWork();
           //发送消息到Handler
           Message m = new Message();
           m.what = 0x111;
           //发送消息
           mHandler.sendMessage(m);
          }
         };
        }.start();
    }
    //模拟一个耗时的操作
    public int doWork()
    {
     //为数组元素赋值
     data[hasData++] = (int) (Math.random()*100);
     try {
   Thread.sleep(100);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return hasData;
    }
2.显示在标题上的进度条
super.onCreate(savedInstanceState);
        //设置窗口特征:启用显示进度的进度条
        requestWindowFeature(Window.FEATURE_PROGRESS);
        //设置窗口特征:启用不显示进度的进度条
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.main);
        Button bn1 = (Button) findViewById(R.id.bn01);
        Button bn2 = (Button) findViewById(R.id.bn02);
        bn1.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    //显示不带进度的进度条
    setProgressBarIndeterminateVisibility(true);
    //显示带进度的进度条
    setProgressBarVisibility(true);
    //设置进度条的进度
    setProgress(4500);
   }
  });
        bn2.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    //隐藏不带进度的进度条
    setProgressBarIndeterminateVisibility(false);
    //隐藏带进度的进度条
    setProgressBarVisibility(false);
   }
  });
五、拖动条SeekBar
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="240px"
        android:src="@drawable/lijiang"/>
   
            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="255"
        android:thumb="@drawable/icon"/>
       
final ImageView image = (ImageView) findViewById(R.id.image);
        SeekBar seekBar = (SeekBar) findViewById(R.id.seekbar);
        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
   @Override
   public void onStopTrackingTouch(SeekBar seekBar) {}
   @Override
   public void onStartTrackingTouch(SeekBar seekBar) {}
   //当拖动条的滑块位置发生改变时触发该方法
   @Override
   public void onProgressChanged(SeekBar seekBar, int progress,
     boolean fromUser)
   {
    image.setAlpha(progress);
   }
  });

六、星级评分条
        android:layout_width="fill_parent"
        android:layout_height="240px"
        android:src="@drawable/lijiang"/>
            android:id="@+id/rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:max="255"
        android:progress="255"
        android:stepSize="0.5"/>
       
        final ImageView image = (ImageView) findViewById(R.id.image);
        RatingBar ratingBar = (RatingBar) findViewById(R.id.rating);
        ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
   //当拖动条的滑块位置发生改变时触发该方法
   @Override
   public void onRatingChanged(RatingBar ratingBar, float rating,
     boolean fromUser)
   {
    //动态改变图片的透明度,其中255是星级评分条的最大值
    //5个星就代表最大值255
    image.setAlpha((int) (rating*255/5));
   }
  });
七、选项卡TabHost
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="xiaoming"/>


    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="zhongming"/>


    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="11pt">
            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="daming"/>

super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        TabHost tabHost = getTabHost();
        //设置使用TabHost布局
        LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(),true);
        //添加第一个标签页
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("已接电话").setContent(R.id.tab01));
        //添加第二个标签页
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("呼出电话",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab02));
        //添加第三个标签页
        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("未接电话").setContent(R.id.tab03));
八、滚动试图ScrollView

    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    android:layout_height="wrap_content">
            android:layout_width="fill_parent"
        android:layout_height="fill_parent">
                    android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="gundongshitugundongshitu"/>
                    android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="gundongshitugundongshitu"/>
       
   



九、列表视图ListView
1.基于数组的ListView
arrays.xml
<?xml version="1.0" encoding="utf-8"?>

   
        梦里花落知多少
        夏末未至
        爱与痛的边缘
        幻城
        猜火车
        三重门
        零下一度
        一座城池
        长安乱
        他的国
        会有天使替我爱你
        冬日最灿烂的阳光
        泡沫之夏
        小魔女的必杀技
        雨夜里的星星沙
        无往而不胜的童话
        午后熏衣茶
   


   

            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/books"
        android:divider="@drawable/red"/>
       
2.  通过ArrayAdapter使用数组来提供列表项

            android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@drawable/green">
       
    ListView list2 = (ListView) findViewById(R.id.list2);
        //定义一个数组
        String[] arr = {"郭敬明","韩寒","明晓溪"};
        //将数组包装ArrayAdapter
  ArrayAdapter arrayAdapter = new ArrayAdapter(this,
    android.R.layout.simple_list_item_1, arr);
        //为ListView设置Adapter
        list2.setAdapter(arrayAdapter);
       
        说明:
        simple_list_item_1每个列表项都是一个普通的TextView
        simple_list_item_2每个列表项都是一个普通的TextView(字体略大)
        simple_list_item_checked每个列表项都是一个已勾选的列表项
        simple_list_item_multiple_choice每个列表项都是带多选框的文本
        simple_list_item_single_choice每个列表项都是带多单选按钮的文本
3.基于ListActivity实现列表
super.onCreate(savedInstanceState);
        //定义一个数组
        String[] arr = {"郭敬明","韩寒","明晓溪"};
        //将数组包装ArrayAdapter
  ArrayAdapter arrayAdapter = new ArrayAdapter(this,
    android.R.layout.simple_list_item_multiple_choice, arr);
        //设置该窗口显示列表
        setListAdapter(arrayAdapter);
4.使用SimpleAdapter创建ListView

            android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
   
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"/>
   
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"/>
       
private String[] names = new String[]{
   "虎头","弄玉","李清照","李白"};
 private int[] imageIds = new int[]{
  R.drawable.tiger,
  R.drawable.nongyu,
  R.drawable.qingzhao,
  R.drawable.libai
 };
 
 super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //创建一个List集合,List集合的元素是Map
        List> listItems = new ArrayList>();
        for(int i = 0; i < names.length;i++)
        {
         Map listItem = new HashMap();
         listItem.put("header", imageIds[i]);
         listItem.put("personName", names[i]);
         listItems.add(listItem);
        }
        //创建一个SimpleAdapter
        SimpleAdapter simpleAdapter = new SimpleAdapter(this
          ,listItems
          ,R.layout.main
          ,new String[]{"personName","header"}
                ,new int[]{R.id.name,R.id.header});
        ListView list = (ListView) findViewById(R.id.mylist);
        //为ListView设置Adapter
        list.setAdapter(simpleAdapter);

更多相关文章

  1. Android(安卓)一个简单的自定义WheelView实现
  2. Android(安卓)自定义控件在Android(安卓)Studio中xmlns不识别
  3. Android之常用类型转换
  4. Android(安卓)自定义控件属性
  5. android 自定义线程,自动结束本身线程
  6. Android中自定义滑动条风格
  7. android 调用手机已安装的音乐播放器 的列表播放音乐
  8. Android解析自定义xml文件--Sax解析xml文件,测试demo(方案二)
  9. 【Android】获得已安装应用

随机推荐

  1. Android跨进程通信IPC之2——Bionic
  2. Android(Lollipop/5.0) Material Design(
  3. android绘图看这篇就够了
  4. Android(安卓)各种布局 控件内部属性大全
  5. Android(安卓)资源文件中@、@android:typ
  6. android中星级评分控件RatingBar的使用
  7. Android(安卓)相对布局属性全集
  8. Android中的主题Theme
  9. Android(安卓)TextView跑马灯效果
  10. Android(安卓)资源文件中@、@android:typ