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

本文实例讲述了Android开发之自定义控件用法。分享给大家供大家参考,具体如下:

今天和大家分享下组合控件的使用。很多时候android自定义控件并不能满足需求,如何做呢?很多方法,可以自己绘制一个,可以通过继承基础控件来重写某些环节,当然也可以将控件组合成一个新控件,这也是最方便的一个方法。今天就来介绍下如何使用组合控件,将通过两个实例来介绍。

第一个实现一个带图片和文字的按钮,如图所示:

整个过程可以分四步走。第一步,定义一个layout,实现按钮内部的布局。代码如下:

custom_button.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
 <ImageView
 android:id="@+id/iv"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center_vertical"
   android:paddingLeft="10.0dip"
   android:paddingTop="10.0dip"
   android:paddingBottom="10.0dip"
   />
 <TextView
 android:id="@+id/tv"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textColor="#ffffff"
   android:layout_marginLeft="8dip"
   android:layout_gravity="center_vertical"
   android:paddingLeft="5.0dip"
   android:paddingTop="10.0dip"
   android:paddingBottom="10.0dip"
   android:paddingRight="10.0dip"
   android:textSize="18.0sp"
   />
</LinearLayout>

这个xml实现一个左图右字的布局,接下来写一个类继承LinearLayout,导入刚刚的布局,并且设置需要的方法,从而使的能在代码中控制这个自定义控件内容的显示。代码如下:

CustomButton.java

package com.szy.customview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class CustomButton extends LinearLayout
{
 private ImageView iv;
 private TextView tv;
 public CustomButton(Context context)
 {
 this(context, null);
 }
 public CustomButton(Context context, AttributeSet attrs)
 {
 super(context, attrs);
 // 导入布局
 LayoutInflater.from(context).inflate(R.layout.custom_button, this, true);
 iv = (ImageView) findViewById(R.id.iv);
 tv = (TextView) findViewById(R.id.tv);
 }
 /**
 * 设置图片资源
 */
 public void setImageResource(int resId)
 {
 iv.setImageResource(resId);
 }
 /**
 * 设置显示的文字
 */
 public void setTextViewText(String text)
 {
 tv.setText(text);
 }
}

第三步,在需要使用这个自定义控件的layout中加入这控件,只需要在xml中加入即可。方法如下:

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical"
 >
 <com.szy.customview.CustomButton
   android:id="@+id/bt_confirm"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/button_bg"
   />
 <com.szy.customview.CustomButton
   android:id="@+id/bt_cancel"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/button_bg"
  />
</LinearLayout>

注意的是,控件标签使用完整的类名即可。为了给按钮一个点击效果,你需要给他一个selector背景,这里就不说了。

最后一步,即在activity中设置该控件的内容。当然,在xml中也可以设置,但是只能设置一个,当我们需要两次使用这样的控件,并且显示内容不同时就不行了。在activity中设置也非常简单,我们在CustomButton这个类中已经写好了相应的方法,简单调用即可。代码如下:

package com.szy.customview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity
{
 private CustomButton btnConfirm;
 private CustomButton btnCancel;
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 btnConfirm = (CustomButton) findViewById(R.id.bt_confirm);
 btnCancel = (CustomButton) findViewById(R.id.bt_cancel);
 btnConfirm.setTextViewText("确定");
 btnConfirm.setImageResource(R.drawable.confirm);
 btnCancel.setTextViewText("取消");
 btnCancel.setImageResource(R.drawable.cancel);
 btnConfirm.setOnClickListener(new OnClickListener()
 {
  @Override
  public void onClick(View v)
  {
  // 在这里可以实现点击事件
  }
 });
 }
}

这样,一个带文字和图片的组合按钮控件就完成了。这样梳理一下,使用还是非常简单的。组合控件能做的事还非常多,主要是在类似上例中的CustomButton类中写好要使用的方法即可。

再来看一个组合控件,带删除按钮的EidtText。即在用户输入后,会出现删除按钮,点击即可取消用户输入。

定义方法和上例一样。首先写一个自定义控件的布局:

custom_editview.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
 <EditText
   android:id="@+id/et"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:singleLine="true"
   />
 <ImageButton
   android:id="@+id/ib"
   android:visibility="gone"
   android:src="@drawable/cancel"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="#00000000"
   android:layout_alignRight="@+id/et" />
</RelativeLayout>

实现输入框右侧带按钮效果,注意将按钮隐藏。然后写一个CustomEditView类,实现删除用户输入功能。这里用到了TextWatch这个接口,监听输入框中的文字变化。使用也很简单,实现他的三个方法即可。看代码:

CustomEditView.java

package com.szy.customview;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class CustomEditView extends LinearLayout implements EdtInterface
{
 ImageButton ib;
 EditText et;
 public CustomEditView(Context context)
 {
 super(context);
 }
 public CustomEditView(Context context, AttributeSet attrs)
 {
 super(context, attrs);
 LayoutInflater.from(context).inflate(R.layout.custom_editview, this, true);
 init();
 }
 private void init()
 {
 ib = (ImageButton) findViewById(R.id.ib);
 et = (EditText) findViewById(R.id.et);
 et.addTextChangedListener(tw);// 为输入框绑定一个监听文字变化的监听器
 // 添加按钮点击事件
 ib.setOnClickListener(new OnClickListener()
 {
  @Override
  public void onClick(View v)
  {
  hideBtn();// 隐藏按钮
  et.setText("");// 设置输入框内容为空
  }
 });
 }
 // 当输入框状态改变时,会调用相应的方法
 TextWatcher tw = new TextWatcher()
 {
 @Override
 public void onTextChanged(CharSequence s, int start, int before, int count)
 {
  // TODO Auto-generated method stub
 }
 @Override
 public void beforeTextChanged(CharSequence s, int start, int count, int after)
 {
  // TODO Auto-generated method stub
 }
 // 在文字改变后调用
 @Override
 public void afterTextChanged(Editable s)
 {
  if (s.length() == 0)
  {
  hideBtn();// 隐藏按钮
  } else
  {
  showBtn();// 显示按钮
  }
 }
 };
 @Override
 public void hideBtn()
 {
 // 设置按钮不可见
 if (ib.isShown())
  ib.setVisibility(View.GONE);
 }
 @Override
 public void showBtn()
 {
 // 设置按钮可见
 if (!ib.isShown())
 {
  ib.setVisibility(View.VISIBLE);
 }
 }
}
interface EdtInterface
{
 public void hideBtn();
 public void showBtn();
}

在TextWatch接口的afterTextChanged方法中对文字进行判断,若长度为0,就隐藏按钮,否则,显示按钮。

另外,实现ImageButton(即那个叉)的点击事件,删除输入框中的内容,并隐藏按钮。

后面两步的实现就是加入到实际布局中:

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical"
 >
 <com.szy.customview.CustomEditView
 android:layout_width="fill_parent"
   android:layout_height="wrap_content"
  />
</LinearLayout>

最后显示效果如图:

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》、《Android视图View技巧总结》、《Android操作SQLite数据库技巧总结》、《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android文件操作技巧汇总》、《Android编程开发之SD卡操作方法汇总》、《Android开发入门与进阶教程》及《Android资源操作技巧汇总》

希望本文所述对大家Android程序设计有所帮助。

(0)

相关推荐

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

    项目需求中有个功能模块需要用到时间选择控件,但是android系统自带的太丑了,只能自己优化下,结合WheelView实现滚轮选择日期,好像网上也挺多这种文章的.但是适用范围还是不同,希望这个能够对需求相同的朋友有一定帮助.控件标题还有年月日时分秒这些可以自己控制是否显示,先来看效果. 1.有年月日时分的开始时间 2.只有年月日的结束时间 3.用于有时身份证到期的时间选择(分为勾选长期和直接选择时间两种,另外长期后面自己也可以进行扩展) 4.项目结构 5.直接贴代码,代码里面注释很详细 <spa

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

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

  • 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&

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

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

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

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

  • 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自定义控件之圆形/圆角的实现代码

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

  • 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自定义倒计时控件示例

    自定义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控件之ProgressBar用法实例分析

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

  • Android自定义播放器控件VideoView

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

随机推荐