简单的自定义控件的编写

本文是一个教程文,学完后,会做出一个简单的自定义控件,至于更复杂的自定义控件,原理都交给你了,怎么做就看自己的大脑了。
这个简单的自定义控件的效果就是一个两个文本框,点击上面文本框,下面文本框显示会改变。

首先需要三个文件:
xxxActivity.java文件+xxx.xml文件+attrrs.xml文件
先说一下三个文件的作用:
xxxActivity的作用:
定义自定义控件中需要被用户控制的组件以及变量,甚至一些用户可能会用到的方法;
xxx.xml的作用:
编写用户需要的UI;
attrrs.xml的作用:
编写用在xml文件中的控制组件的自定义的属性

然后上代码,上一个代码解释一个。
xml文件的代码就是简单的放进去两个TextView:
zdy_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <View
        android:id="@+id/zdy_tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
		android:text="1111111" />

    <View
        android:id="@+id/zdy_tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
		android:text="look" />
</LinearLayout>

然后是在上attrs.xml文件(这个就是你用在xml里面的自定义属性):
attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name=ZdyLayout">
        <attr name="click" format="string" />
    </declare-styleable>
</resources>

这里简单解释一下,这就是定义了一个自定义的xml文件里面属性,属性名叫click,类型是string的,存储这个属性串的xml的别名叫做ZdyLayout。
接下来重头戏,上类文件:
ZdyLayout.java:

public class ZdyLayout extends FrameLayout {
	private View mView;
	private TextView tv1;
	private TextView tv2;

	private void init(Context context,AttributeSet attrs){
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mView = inflater.inflate(R.layout.zdy_activity,this,true);
		TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MenuItemLayout);
		tv1 = mView.findViewById(R.id.zdy_tv1);
		tv2 = mView.findViewById(R.id.zdy_tv2);
		tv1.setText(a.getString(R.styleable.MenuItemLayout_click));
	}
	//tv1和tv2的setget方法,这里省略;
	//下面是继承FrameLayout后需要实现的方法,
	//实现四个的话,参数最多的那个不用改变,实现三个的话,就是下面的代码
	public ZdyLayout(Context context){
		this(context,null);
	}
	public ZdyLayout(Context context,AttributeSet attrs){
		this(context,attrs,0);
	}
	public ZdyLayout(Context context,AttrubuteSet attrs,int defStyleAttr){
		super(context,attrs,defStyleAttr);
		init(context,attrs);
	}
}

至此,自定义控件完成,那么怎么使用呢?
先写一个程序入口类的布局文件,在这个文件中使用这个控件,下面方法使用这个控件的代码:

<com.myth.zframe.zidingyi.ZdyLayout
	android:id="@id/test"
	app:click="点我"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	/>

上面代码很简单,但是多了一个app:click这个属性,这个就是上面attrs里面我们自定义的属性,我们为这个属性传入了一个“点我” 的字符串。
然后我们看一下我们的ZdyLayout中init方法:

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MenuItemLayout);

这一句将attrs里面的定义拿出来;

tv1.setText(a.getString(R.styleable.MenuItemLayout_click));

这一句去使用这个属性的传入值。
现在明白自定义控件的自定义属性怎么编写了吧。
最后我们来写一个程序入口类:

public class MainActivity extends AppCompatActivity{
	private ZdyLayout layout;
	@Override
	protected void OnCreate(Bundle saveInstanceState){
		super.onCreate(saveInstanceState);
		setContentView(R.layout.zdy_activity);
		initRes();
	}
	private void initRes(){
		layout = findeViewById(R.id.test);
		layout.getTv1().setOnClickListener(new View.onClickListener(){
			@Override
			public void onClick(View view){
				layout.gettv2.setText("2222");
			}
		}):
	}
}

至此,使用自定义控件也已经结束,我们运行一下点击“点我”,发现下面的TextView变成了2222,
这就是我们写的简单的自定义控件。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinForm 是微软提供的用于创建 Windows 应用程序的框架,它提供了许多常用的控件,如按钮、标签、文本框等。但是,在实际开发中,我们可能需要自己编写一些特定的控件来实现一些特殊的功能。这时,我们就需要自定义控件。 以下是自定义 WinForm 控件的步骤: 1.创建一个类库项目 首先,我们需要创建一个类库项目,用于存放我们自定义的控件类。在 Visual Studio 中,可以通过“文件”->“新建”->“项目”->“类库”来创建。 2.添加引用 在类库项目中,我们需要添加对 System.Windows.Forms 的引用,这样才能够使用 WinForm 控件。 3.创建控件类 在类库项目中,我们可以创建一个控件类,该类继承自 System.Windows.Forms.Control。 例如,我们要编写一个自定义的按钮控件,可以创建一个类 ButtonEx,如下所示: ```csharp public class ButtonEx : Control { //控件的状态 private bool _isMouseOver = false; private bool _isMouseDown = false; public ButtonEx() { //设置控件的基本属性 this.SetStyle(ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.DoubleBuffered = true; this.BackColor = Color.Transparent; this.ForeColor = Color.Black; this.Font = new Font("宋体", 9f, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))); this.Size = new Size(75, 23); } //重写基类的 OnPaint 方法,绘制控件 protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); //绘制按钮的背景和边框 if (_isMouseDown) { e.Graphics.FillRectangle(Brushes.LightGray, this.ClientRectangle); } else if (_isMouseOver) { e.Graphics.FillRectangle(Brushes.Gray, this.ClientRectangle); } else { e.Graphics.FillRectangle(Brushes.White, this.ClientRectangle); } e.Graphics.DrawRectangle(Pens.Black, new Rectangle(0, 0, this.Width - 1, this.Height - 1)); //绘制按钮的文本 StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Center; stringFormat.LineAlignment = StringAlignment.Center; e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), this.ClientRectangle, stringFormat); } //重写基类的 OnMouseEnter 方法,处理鼠标进入控件的事件 protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); _isMouseOver = true; this.Invalidate(); } //重写基类的 OnMouseLeave 方法,处理鼠标离开控件的事件 protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); _isMouseOver = false; this.Invalidate(); } //重写基类的 OnMouseDown 方法,处理鼠标按下控件的事件 protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); _isMouseDown = true; this.Invalidate(); } //重写基类的 OnMouseUp 方法,处理鼠标松开控件的事件 protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); _isMouseDown = false; this.Invalidate(); } } ``` 在上面的代码中,我们重写了基类的 OnPaint、OnMouseEnter、OnMouseLeave、OnMouseDown 和 OnMouseUp 方法,分别处理了控件的绘制、鼠标进入、鼠标离开、鼠标按下和鼠标松开事件。在 OnPaint 方法中,我们使用 Graphics 对象绘制了控件的背景、边框和文本。 4.将控件添加到工具箱 完成自定义控件编写后,我们需要将它添加到工具箱中,以便在 WinForm 窗体设计器中使用。 在类库项目中,右键单击“引用”->“添加引用”,然后选择“浏览”,找到刚才创建的类库项目生成的 DLL 文件,添加引用。 接着,在类库项目中,右键单击“ButtonEx.cs”文件,选择“属性”,将“生成操作”设置为“编译”。 然后,打开 WinForm 窗体设计器,右键单击工具箱中的空白区域,选择“选择项”,在“选择工具箱项”对话框中选择“浏览”,找到刚才创建的类库项目生成的 DLL 文件,添加到工具箱中。 这样,我们就可以在 WinForm 窗体设计器中使用自定义的控件了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值