轻松实现可扩展自定义的Android滚轮时间选择控件

项目需求中有个功能模块需要用到时间选择控件,但是android系统自带的太丑了,只能自己优化下,结合WheelView实现滚轮选择日期,好像网上也挺多这种文章的。但是适用范围还是不同,希望这个能够对需求相同的朋友有一定帮助。控件标题还有年月日时分秒这些可以自己控制是否显示,先来看效果。

1.有年月日时分的开始时间

2.只有年月日的结束时间

3.用于有时身份证到期的时间选择(分为勾选长期和直接选择时间两种,另外长期后面自己也可以进行扩展)

4.项目结构

5.直接贴代码,代码里面注释很详细

 <span style="font-size:18px;"><span style="font-size:14px;">package com.andrew.datechoosewheelviewdemo;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andrew.datechoosewheelviewdemo.widget.OnWheelChangedListener;
import com.andrew.datechoosewheelviewdemo.widget.OnWheelScrollListener;
import com.andrew.datechoosewheelviewdemo.widget.WheelView;
import com.andrew.datechoosewheelviewdemo.widget.adapters.AbstractWheelTextAdapter;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * 使用说明:1.showLongTerm()是否显示长期选项
 * 2.setTimePickerGone隐藏时间选择
 * 3.接口DateChooseInterface
 *
 * 用于时间日期的选择
 * Created by liuhongxia on 2016/4/16.
 */
public class DateChooseWheelViewDialog extends Dialog implements View.OnClickListener {
 //控件
 private WheelView mYearWheelView;
 private WheelView mDateWheelView;
 private WheelView mHourWheelView;
 private WheelView mMinuteWheelView;
 private CalendarTextAdapter mDateAdapter;
 private CalendarTextAdapter mHourAdapter;
 private CalendarTextAdapter mMinuteAdapter;
 private CalendarTextAdapter mYearAdapter;
 private TextView mTitleTextView;
 private Button mSureButton;
 private Dialog mDialog;
 private Button mCloseDialog;
 private LinearLayout mLongTermLayout;
 private TextView mLongTermTextView;

 //变量
 private ArrayList<String> arry_date = new ArrayList<String>();
 private ArrayList<String> arry_hour = new ArrayList<String>();
 private ArrayList<String> arry_minute = new ArrayList<String>();
 private ArrayList<String> arry_year = new ArrayList<String>();

 private int nowDateId = 0;
 private int nowHourId = 0;
 private int nowMinuteId = 0;
 private int nowYearId = 0;
 private String mYearStr;
 private String mDateStr;
 private String mHourStr;
 private String mMinuteStr;
 private boolean mBlnBeLongTerm = false;//是否需要长期
 private boolean mBlnTimePickerGone = false;//时间选择是否显示

 //常量
 private final int MAX_TEXT_SIZE = 18;
 private final int MIN_TEXT_SIZE = 16;

 private Context mContext;
 private DateChooseInterface dateChooseInterface;

 public DateChooseWheelViewDialog(Context context, DateChooseInterface dateChooseInterface) {
 super(context);
 this.mContext = context;
 this.dateChooseInterface = dateChooseInterface;
 mDialog = new Dialog(context,R.style.dialog);
 initView();
 initData();
 }

 private void initData() {
 initYear();
 initDate();
 initHour();
 initMinute();
 initListener();
 }

 /**
 * 初始化滚动监听事件
 */
 private void initListener() {
 //年份*****************************
 mYearWheelView.addChangingListener(new OnWheelChangedListener() {

  @Override
  public void onChanged(WheelView wheel, int oldValue, int newValue) {
  String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem());
  setTextViewStyle(currentText, mYearAdapter);
  mYearStr = arry_year.get(wheel.getCurrentItem()) + "";
  }
 });

 mYearWheelView.addScrollingListener(new OnWheelScrollListener() {

  @Override
  public void onScrollingStarted(WheelView wheel) {

  }

  @Override
  public void onScrollingFinished(WheelView wheel) {
  String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem());
  setTextViewStyle(currentText, mYearAdapter);
  }
 });

 //日期********************
 mDateWheelView.addChangingListener(new OnWheelChangedListener() {

  @Override
  public void onChanged(WheelView wheel, int oldValue, int newValue) {
  String currentText = (String) mDateAdapter.getItemText(wheel.getCurrentItem());
  setTextViewStyle(currentText, mDateAdapter);
//  mDateCalendarTextView.setText(" " + arry_date.get(wheel.getCurrentItem()));
  mDateStr = arry_date.get(wheel.getCurrentItem());
  }
 });

 mDateWheelView.addScrollingListener(new OnWheelScrollListener() {

  @Override
  public void onScrollingStarted(WheelView wheel) {

  }

  @Override
  public void onScrollingFinished(WheelView wheel) {
  String currentText = (String) mDateAdapter.getItemText(wheel.getCurrentItem());
  setTextViewStyle(currentText, mDateAdapter);
  }
 });

 //小时***********************************
 mHourWheelView.addChangingListener(new OnWheelChangedListener() {

  @Override
  public void onChanged(WheelView wheel, int oldValue, int newValue) {
  String currentText = (String) mHourAdapter.getItemText(wheel.getCurrentItem());
  setTextViewStyle(currentText, mHourAdapter);
  mHourStr = arry_hour.get(wheel.getCurrentItem()) + "";
  }
 });

 mHourWheelView.addScrollingListener(new OnWheelScrollListener() {

  @Override
  public void onScrollingStarted(WheelView wheel) {

  }

  @Override
  public void onScrollingFinished(WheelView wheel) {
  String currentText = (String) mHourAdapter.getItemText(wheel.getCurrentItem());
  setTextViewStyle(currentText, mHourAdapter);
  }
 });

 //分钟********************************************
 mMinuteWheelView.addChangingListener(new OnWheelChangedListener() {

  @Override
  public void onChanged(WheelView wheel, int oldValue, int newValue) {
  String currentText = (String) mMinuteAdapter.getItemText(wheel.getCurrentItem());
  setTextViewStyle(currentText, mMinuteAdapter);
  mMinuteStr = arry_minute.get(wheel.getCurrentItem()) + "";
  }
 });

 mMinuteWheelView.addScrollingListener(new OnWheelScrollListener() {

  @Override
  public void onScrollingStarted(WheelView wheel) {

  }

  @Override
  public void onScrollingFinished(WheelView wheel) {
  String currentText = (String) mMinuteAdapter.getItemText(wheel.getCurrentItem());
  setTextViewStyle(currentText, mMinuteAdapter);
  }
 });
 }

 /**
 * 初始化分钟
 */
 private void initMinute() {
 Calendar nowCalendar = Calendar.getInstance();
 int nowMinite = nowCalendar.get(Calendar.MINUTE);
 arry_minute.clear();
 for (int i = 0; i <= 59; i++) {
  arry_minute.add(i + "");
  if (nowMinite == i){
  nowMinuteId = arry_minute.size() - 1;
  }
 }

 mMinuteAdapter = new CalendarTextAdapter(mContext, arry_minute, nowMinuteId, MAX_TEXT_SIZE, MIN_TEXT_SIZE);
 mMinuteWheelView.setVisibleItems(5);
 mMinuteWheelView.setViewAdapter(mMinuteAdapter);
 mMinuteWheelView.setCurrentItem(nowMinuteId);
 mMinuteStr = arry_minute.get(nowMinuteId) + "";
 setTextViewStyle(mMinuteStr, mMinuteAdapter);

 }

 /**
 * 初始化时间
 */
 private void initHour() {
 Calendar nowCalendar = Calendar.getInstance();
 int nowHour = nowCalendar.get(Calendar.HOUR_OF_DAY);
 arry_hour.clear();
 for (int i = 0; i <= 23; i++) {
  arry_hour.add(i + "");
  if (nowHour == i){
  nowHourId = arry_hour.size() - 1;
  }
 }

 mHourAdapter = new CalendarTextAdapter(mContext, arry_hour, nowHourId, MAX_TEXT_SIZE, MIN_TEXT_SIZE);
 mHourWheelView.setVisibleItems(5);
 mHourWheelView.setViewAdapter(mHourAdapter);
 mHourWheelView.setCurrentItem(nowHourId);
 mHourStr = arry_hour.get(nowHourId) + "";
 setTextViewStyle(mHourStr, mHourAdapter);
 }

 /**
 * 初始化年
 */
 private void initYear() {
 Calendar nowCalendar = Calendar.getInstance();
 int nowYear = nowCalendar.get(Calendar.YEAR);
 arry_year.clear();
 for (int i = 0; i <= 99; i++) {
  int year = nowYear -30 + i;
  arry_year.add(year + "年");
  if (nowYear == year) {
  nowYearId = arry_year.size() - 1;
  }
 }
 mYearAdapter = new CalendarTextAdapter(mContext, arry_year, nowYearId, MAX_TEXT_SIZE, MIN_TEXT_SIZE);
 mYearWheelView.setVisibleItems(5);
 mYearWheelView.setViewAdapter(mYearAdapter);
 mYearWheelView.setCurrentItem(nowYearId);
 mYearStr = arry_year.get(nowYearId);
 }

 private void initView() {
 View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_date_choose, null);
 mDialog.setContentView(view);
 mYearWheelView = (WheelView) view.findViewById(R.id.year_wv);
 mDateWheelView = (WheelView) view.findViewById(R.id.date_wv);
 mHourWheelView = (WheelView) view.findViewById(R.id.hour_wv);
 mMinuteWheelView = (WheelView) view.findViewById(R.id.minute_wv);
 mTitleTextView = (TextView) view.findViewById(R.id.title_tv);
 mSureButton = (Button) view.findViewById(R.id.sure_btn);
 mCloseDialog = (Button) view.findViewById(R.id.date_choose_close_btn);
 mLongTermLayout = (LinearLayout) view.findViewById(R.id.long_term_layout);
 mLongTermTextView = (TextView) view.findViewById(R.id.long_term_tv);

 mSureButton.setOnClickListener(this);
 mCloseDialog.setOnClickListener(this);
 mLongTermTextView.setOnClickListener(this);
 }

 /**
 * 初始化日期
 */
 private void initDate() {
 Calendar nowCalendar = Calendar.getInstance();
 int nowYear = nowCalendar.get(Calendar.YEAR);
 arry_date.clear();
 setDate(nowYear);
 mDateAdapter = new CalendarTextAdapter(mContext, arry_date, nowDateId, MAX_TEXT_SIZE, MIN_TEXT_SIZE);
 mDateWheelView.setVisibleItems(5);
 mDateWheelView.setViewAdapter(mDateAdapter);
 mDateWheelView.setCurrentItem(nowDateId);

 mDateStr = arry_date.get(nowDateId);
 setTextViewStyle(mDateStr, mDateAdapter);
 }

 public void setDateDialogTitle(String title) {
 mTitleTextView.setText(title);
 }

 public void setTimePickerGone(boolean isGone) {
 mBlnTimePickerGone = isGone;
 if (isGone) {
  LinearLayout.LayoutParams yearParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT,
   LinearLayout.LayoutParams.WRAP_CONTENT);
  yearParams.rightMargin = 22;

  LinearLayout.LayoutParams dateParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT,
   LinearLayout.LayoutParams.WRAP_CONTENT);
  mYearWheelView.setLayoutParams(yearParams);
  mDateWheelView.setLayoutParams(dateParams);

  mHourWheelView.setVisibility(View.GONE);
  mMinuteWheelView.setVisibility(View.GONE);
 } else {
  mHourWheelView.setVisibility(View.VISIBLE);
  mMinuteWheelView.setVisibility(View.VISIBLE);
 }

 }

 public void showLongTerm(boolean show) {
 if (show) {
  mLongTermLayout.setVisibility(View.VISIBLE);
 } else {
  mLongTermLayout.setVisibility(View.GONE);
 }

 }

 /**
 * 将改年的所有日期写入数组
 * @param year
 */
 private void setDate(int year){
 boolean isRun = isRunNian(year);
 Calendar nowCalendar = Calendar.getInstance();
 int nowMonth = nowCalendar.get(Calendar.MONTH) + 1;
 int nowDay = nowCalendar.get(Calendar.DAY_OF_MONTH);
 for (int month = 1; month <= 12; month++){
  switch (month){
  case 1:
  case 3:
  case 5:
  case 7:
  case 8:
  case 10:
  case 12:
   for (int day = 1; day <= 31; day++){
   arry_date.add(month + "月" + day + "日");

   if (month == nowMonth && day == nowDay){
    nowDateId = arry_date.size() - 1;
   }
   }
   break;
  case 2:
   if (isRun){
   for (int day = 1; day <= 29; day++){
    arry_date.add(month + "月" + day + "日");
    if (month == nowMonth && day == nowDay){
    nowDateId = arry_date.size() - 1;
    }
   }
   }else {
   for (int day = 1; day <= 28; day++){
    arry_date.add(month + "月" + day + "日");
    if (month == nowMonth && day == nowDay){
    nowDateId = arry_date.size() - 1;
    }
   }
   }
   break;
  case 4:
  case 6:
  case 9:
  case 11:
   for (int day = 1; day <= 30; day++){
   arry_date.add(month + "月" + day + "日");
   if (month == nowMonth && day == nowDay){
    nowDateId = arry_date.size() - 1;
   }
   }
   break;
  default:
   break;
  }
 }
 }

 /**
 * 判断是否是闰年
 * @param year
 * @return
 */
 private boolean isRunNian(int year){
 if(year % 4 == 0 && year % 100 !=0 || year % 400 == 0){
  return true;
 }else {
  return false;
 }
 }

 /**
 * 设置文字的大小
 * @param curriteItemText
 * @param adapter
 */
 public void setTextViewStyle(String curriteItemText, CalendarTextAdapter adapter) {
 ArrayList<View> arrayList = adapter.getTestViews();
 int size = arrayList.size();
 String currentText;
 for (int i = 0; i < size; i++) {
  TextView textvew = (TextView) arrayList.get(i);
  currentText = textvew.getText().toString();
  if (curriteItemText.equals(currentText)) {
  textvew.setTextSize(MAX_TEXT_SIZE);
  textvew.setTextColor(mContext.getResources().getColor(R.color.text_10));
  } else {
  textvew.setTextSize(MIN_TEXT_SIZE);
  textvew.setTextColor(mContext.getResources().getColor(R.color.text_11));
  }
 }
 }

 @Override
 public void onClick(View v) {
 switch (v.getId()) {
  case R.id.sure_btn://确定选择按钮监听
  if (mBlnTimePickerGone) {
   dateChooseInterface.getDateTime(strTimeToDateFormat(mYearStr, mDateStr), mBlnBeLongTerm);
  } else {
   dateChooseInterface.getDateTime(strTimeToDateFormat(mYearStr, mDateStr , mHourStr , mMinuteStr), mBlnBeLongTerm);
  }
  dismissDialog();
  break;
  case R.id.date_choose_close_btn://关闭日期选择对话框
  dismissDialog();
  break;
  case R.id.long_term_tv://选择长期时间监听
  if (!mBlnBeLongTerm) {
   mLongTermTextView.setBackgroundResource(R.drawable.gouxuanok);
   mBlnBeLongTerm = true;
  } else {
   mLongTermTextView.setBackgroundResource(R.drawable.gouxuanno);
   mBlnBeLongTerm = false;
  }
  default:
  break;
 }
 }

 /**
 * 对话框消失
 */
 private void dismissDialog() {

 if (Looper.myLooper() != Looper.getMainLooper()) {

  return;
 }

 if (null == mDialog || !mDialog.isShowing() || null == mContext
  || ((Activity) mContext).isFinishing()) {

  return;
 }

 mDialog.dismiss();
 this.dismiss();
 }

 /**
 * 显示日期选择dialog
 */
 public void showDateChooseDialog() {

 if (Looper.myLooper() != Looper.getMainLooper()) {

  return;
 }

 if (null == mContext || ((Activity) mContext).isFinishing()) {

  // 界面已被销毁
  return;
 }

 if (null != mDialog) {

  mDialog.show();
  return;
 }

 if (null == mDialog) {

  return;
 }

 mDialog.setCanceledOnTouchOutside(true);
 mDialog.show();
 }

 /**
 * xx年xx月xx日xx时xx分转成yyyy-MM-dd HH:mm
 * @param yearStr
 * @param dateStr
 * @param hourStr
 * @param minuteStr
 * @return
 */
 private String strTimeToDateFormat(String yearStr, String dateStr, String hourStr, String minuteStr) {

 return yearStr.replace("年", "-") + dateStr.replace("月", "-").replace("日", " ")
  + hourStr + ":" + minuteStr;
 }

 private String strTimeToDateFormat(String yearStr, String dateStr) {

 return yearStr.replace("年", "-") + dateStr.replace("月", "-").replace("日", "");
 }

 /**
 * 滚轮的adapter
 */
 private class CalendarTextAdapter extends AbstractWheelTextAdapter {
 ArrayList<String> list;

 protected CalendarTextAdapter(Context context, ArrayList<String> list, int currentItem, int maxsize, int minsize) {
  super(context, R.layout.item_birth_year, R.id.tempValue, currentItem, maxsize, minsize);
  this.list = list;
 }

 @Override
 public View getItem(int index, View cachedView, ViewGroup parent) {
  View view = super.getItem(index, cachedView, parent);
  return view;
 }

 @Override
 public int getItemsCount() {
  return list.size();
 }

 @Override
 protected CharSequence getItemText(int index) {
  String str = list.get(index) + "";
  return str;
 }
 }

 /**
 * 回调选中的时间(默认时间格式"yyyy-MM-dd HH:mm:ss")
 */
 public interface DateChooseInterface{
 void getDateTime(String time, boolean longTimeChecked);
 }

}
</span></span>

 6.MainActivity里面使用

<span style="font-size:18px;"><span style="font-size:14px;">package com.andrew.datechoosewheelviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener {
 private Button mStartDateButton;
 private Button mEndDateButton;
 private Button mDateValidButton;
 private TextView mShowContentTextView;

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

 mStartDateButton = (Button) this.findViewById(R.id.start_date_btn);
 mEndDateButton = (Button) this.findViewById(R.id.end_date_btn);
 mDateValidButton = (Button) this.findViewById(R.id.date_valid_btn);
 mShowContentTextView = (TextView) this.findViewById(R.id.show_content_tv);

 mStartDateButton.setOnClickListener(this);
 mEndDateButton.setOnClickListener(this);
 mDateValidButton.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
 switch (v.getId()) {
  case R.id.start_date_btn://开始时间
  DateChooseWheelViewDialog startDateChooseDialog = new DateChooseWheelViewDialog(MainActivity.this, new DateChooseWheelViewDialog.DateChooseInterface() {
   @Override
   public void getDateTime(String time, boolean longTimeChecked) {
   mShowContentTextView.setText(time);
   }
  });
  startDateChooseDialog.setDateDialogTitle("开始时间");
  startDateChooseDialog.showDateChooseDialog();
  break;
  case R.id.end_date_btn://结束时间
  DateChooseWheelViewDialog endDateChooseDialog = new DateChooseWheelViewDialog(MainActivity.this,
   new DateChooseWheelViewDialog.DateChooseInterface() {
    @Override
    public void getDateTime(String time, boolean longTimeChecked) {
    mShowContentTextView.setText(time);
    }
   });
  endDateChooseDialog.setTimePickerGone(true);
  endDateChooseDialog.setDateDialogTitle("结束时间");
  endDateChooseDialog.showDateChooseDialog();
  break;
  case R.id.date_valid_btn://身份证有效期
  DateChooseWheelViewDialog dateValidChooseDialog = new DateChooseWheelViewDialog(MainActivity.this,
   new DateChooseWheelViewDialog.DateChooseInterface() {
    @Override
    public void getDateTime(String time, boolean longTimeChecked) {
    if (longTimeChecked) {
     mShowContentTextView.setText("长期 ");
    } else {
     mShowContentTextView.setText(time);
    }
    }
   });
  dateValidChooseDialog.setTimePickerGone(true);
  dateValidChooseDialog.showLongTerm(true);
  dateValidChooseDialog.setDateDialogTitle("身份证到期时间");
  dateValidChooseDialog.showDateChooseDialog();
  break;
  default:
  break;
 }

 }
}
</span></span>

代码下载地址:
github:https://github.com/hongxialiu/DateChooseWheelViewDemo 
源码下载: WheelView滚轮时间选择控件

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android控件之ProgressBar用法实例分析

    本文实例讲述了Android控件之ProgressBar用法.分享给大家供大家参考.具体如下: ProgressBar位于android.widget包下,其继承于View,主要用于显示一些操作的进度.应用程序可以修改其长度表示当前后台操作的完成情况.因为进度条会移动,所以长时间加载某些资源或者执行某些耗时的操作时,不会使用户界面失去响应.ProgressBar类的使用非常简单,只需将其显示到前台,然后启动一个后台线程定时更改表示进度的数值即可. 以下ProgressBar跟Handle结合,模

  • Android自定义播放器控件VideoView

    介绍 最近要使用播放器做一个简单的视频播放功能,开始学习VideoView,在横竖屏切换的时候碰到了点麻烦,不过在查阅资料后总算是解决了.在写VideoView播放视频时候定义控制的代码全写在Actvity里了,写完一看我靠代码好乱,于是就写了个自定义的播放器控件,支持指定大小,可以横竖屏切换,手动左右滑动快进快退.好了,下面开始. 效果图有点卡,我也不知道为啥..... VideoView介绍 这个是我们实现视频播放最主要的控件,详细的介绍大家百度就去看,这里介绍几个常用的方法. 用于播放视频

  • Android开发中自定义ProgressBar控件的方法示例

    本文实例讲述了Android开发中自定义ProgressBar控件的方法.分享给大家供大家参考,具体如下: 很简单,首先加载Drawable,在onMeasure设置好其区域大小, 然后使用canvas.clipRect绘图 public class ProgressView extends ImageView { private Drawable maskDraw; /** * 加载的进度 0-100 */ private int mProcess = 20; public ProgressV

  • Android自定义控件实现可左右滑动的导航条

    先上效果图: 这个控件其实算是比较轻量级的,相信不少小伙伴都能做出来.因为项目中遇到了一些特殊的定制要求,所以就自己写了一个,这里放出来.  首先来分析下这个控件的功能:  •能够响应左右滑动,并且能响应快速滑动 •选择项和未选择项有不同的样式表现,比如前景色,背景色,字体大小变粗之内的 •在切换选项的时候,如果当前选项未完全呈现在界面前,则自动滚动直至当前选项完全暴露显示 前两条还有,简简单单就实现了,主要是第三点,这才是我自定义这个控件的原因!那么如果要实现这个控件,需要用到哪些知识呢? 

  • Android控件之RatingBar自定义星级评分样式

    一.RatingBar简单介绍 RatingBar是基于SeekBar(拖动条)和ProgressBar(状态条)的扩展,用星形来显示等级评定,在使用默认RatingBar时,用户可以通过触摸/拖动/按键(比如遥控器)来设置评分, RatingBar自带有两种模式 ,一个小风格 ratingBarStyleSmall,大风格为ratingBarStyleIndicator,大的只适合做指示,不适用与用户交互. 效果图展示: 二.实例 1.布局文件 <?xml version="1.0&qu

  • android开发教程之自定义控件checkbox的样式示例

    主界面xml文件 复制代码 代码如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_p

  • Android开发之自定义控件用法详解

    本文实例讲述了Android开发之自定义控件用法.分享给大家供大家参考,具体如下: 今天和大家分享下组合控件的使用.很多时候android自定义控件并不能满足需求,如何做呢?很多方法,可以自己绘制一个,可以通过继承基础控件来重写某些环节,当然也可以将控件组合成一个新控件,这也是最方便的一个方法.今天就来介绍下如何使用组合控件,将通过两个实例来介绍. 第一个实现一个带图片和文字的按钮,如图所示: 整个过程可以分四步走.第一步,定义一个layout,实现按钮内部的布局.代码如下: custom_bu

  • android自定义倒计时控件示例

    自定义TextView控件TimeTextView代码: 复制代码 代码如下: import android.content.Context;import android.content.res.TypedArray;import android.graphics.Paint;import android.text.Html;import android.util.AttributeSet;import android.widget.TextView; import com.new0315.R;

  • Android自定义控件之圆形/圆角的实现代码

    一.问题在哪里? 问题来源于app开发中一个很常见的场景--用户头像要展示成圆的:  二.怎么搞? 机智的我,第一想法就是,切一张中间圆形透明.四周与底色相同.尺寸与头像相同的蒙板图片,盖在头像上不就完事了嘛,哈哈哈! 在背景纯色的前提下,这的确能简单解决问题,但是如果背景没有这么简单呢? 在这种不规则背景下,有两个问题: 1).背景图常常是适应手机宽度缩放,而头像的尺寸又是固定宽高DP的,所以固定的蒙板图片是没法保证在不同机型上都和背景图案吻合的. 2).在这种非纯色背景下,哪天想调整一下头像

  • android ListView和ProgressBar(进度条控件)的使用方法

    ListView控件的使用:ListView控件里面装的是一行一行的数据,一行中可能有多列,选中一行,则该行的几列都被选中,同时可以触发一个事件,这种控件在平时还是用得很多的.使用ListView时主要是要设置一个适配器,适配器主要是用来放置一些数据.使用起来稍微有些复杂,这里用的是android自带的SimpleAdapter,形式如下:android.widget.SimpleAdapter.SimpleAdapter(Context context, List<? extends Map<

  • Android进度条控件progressbar使用方法详解

    一.简介 二.方法 1)进度条ProgressBar使用方法 1.在layout布局文件中创建ProgressBar控件 <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:progress="30&

随机推荐