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

 更新时间:2016年08月12日 11:49:53   作者:wop_niaoren19870227  
这篇文章主要介绍了Android开发之自定义控件用法,结合实例形式分析了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程序设计有所帮助。

相关文章

  • Android实现加载对话框

    Android实现加载对话框

    这篇文章主要为大家详细介绍了Android实现加载对话框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-01-01
  • Android 编译出错版本匹配问题解决办法

    Android 编译出错版本匹配问题解决办法

    这篇文章主要介绍了Android 编译出错 app\build\intermediates\res\merged\debug\values-v23\values-v23.xml 的问题解决办法,需要的朋友可以参考下
    2017-07-07
  • Android自定义PopupWindow小案例

    Android自定义PopupWindow小案例

    这篇文章主要为大家详细介绍了Android自定义PopupWindow小案例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • Android 自定义按钮点击事件和长按事件对比

    Android 自定义按钮点击事件和长按事件对比

    这篇文章主要介绍了 Android 自定义按钮点击事件和长按事件对比的相关资料,需要的朋友可以参考下
    2017-04-04
  • Android studio无法创建类和接口和提示问题的完美解决办法

    Android studio无法创建类和接口和提示问题的完美解决办法

    这篇文章主要介绍了Android studio无法创建类和接口和提示问题解决办法,内容比较简单,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2018-04-04
  • Kotlin Extension Function扩展函数详细介绍

    Kotlin Extension Function扩展函数详细介绍

    Kotlin支持使用新功能扩展类的能力,而无需通过类实现继承概念或使用设计模式,如装饰器(Decorator)。这是通过称为扩展功能(Extension Function)的特殊方式来完成的。因此,此功能可以有效地使代码变得更清晰和易于阅读,并且还可以减少代码
    2023-02-02
  • flutter 自定义websocket路由的实现

    flutter 自定义websocket路由的实现

    这篇文章主要介绍了flutter 自定义websocket路由的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • Android 实现定时任务的过程详解

    Android 实现定时任务的过程详解

    这篇文章主要介绍了Android 定时任务过程详解的相关资料,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • 详解Android中的ActivityThread和APP启动过程

    详解Android中的ActivityThread和APP启动过程

    ActivityThread就是我们常说的主线程或UI线程,ActivityThread的main方法是整个APP的入口,本篇深入学习下ActivityThread,顺便了解下APP和Activity的启动过程。
    2021-06-06
  • android viewpager实现轮播效果

    android viewpager实现轮播效果

    这篇文章主要为大家详细介绍了android viewpager实现轮播效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06

最新评论