Flex中的TextInput (5):添加焦点效果

2024-04-03 09:18
文章标签 效果 焦点 flex textinput

本文主要是介绍Flex中的TextInput (5):添加焦点效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Flex中的TextInput控件在获得焦点的时候,输入框周围会出现蓝色的边框,当失去焦点的时候,蓝色边框消失。

其原理就是用一个Shape类画出蓝色边框。当TextInput获得焦点的时候,通过addChild把该Shape类对象加入显示列表;当TextInput失去焦点的时候,通过removedChild把该Shape类对象从显示列表中删除。

 

TextInputComponent.as

package {
import Component.MyTextInput;	
import flash.display.Sprite;
public class TextInputComponent extends Sprite
{
public function TextInputComponent()
{
var txt1:MyTextInput = new MyTextInput(100,100,200,20);
addChild(txt1);
var txt2:MyTextInput = new MyTextInput(100,130,300,15);
addChild(txt2);
}
}
}


MyTextInput.as

package Component
{
import flash.display.Sprite;
import flash.events.FocusEvent;
import flash.text.TextField;
import flash.text.TextFieldType;
import skins.Border;
import skins.FocusRect;
public class MyTextInput extends Sprite
{
private var _w:int;
private var _h:int;
private var _x:int;
private var _y:int;
private var _focusRect:FocusRect;
public function MyTextInput(x:int,y:int,width:int,height:int)
{
_x = x;
_y = y;
_w = width;
_h = height;
//设定焦点矩形
_focusRect = new FocusRect(_x-2,_y-2,_w+4,_h+4);
//监听焦点事件
addEventListener(FocusEvent.FOCUS_IN, focusInHandler);
addEventListener(FocusEvent.FOCUS_OUT, focusOutHandler);
CreateChild();
}
public function CreateChild():void{
//添加3D边框
var border:Border = new Border(_x,_y,_w,_h);
addChild(border);
//添加TextField
var txt:TextField = new TextField();
txt.type = TextFieldType.INPUT;
txt.x = _x + 1;
txt.y = _y + 1;
txt.width = _w-2;
txt.height = _h-2;
txt.background = true;
txt.backgroundColor = 0xffffff;
addChild(txt);
}
protected function focusInHandler(event:FocusEvent):void
{
addChild(_focusRect);
}
protected function focusOutHandler(event:FocusEvent):void
{
removeChild(_focusRect);
}
}
}


Border.as

package skins
{
import flash.display.Graphics;
import flash.display.Shape;
import mx.utils.ColorUtil;
public class Border extends Shape
{
//定义高度和宽度
private var _w:int;
private var _h:int;
private var _x:int;
private var _y:int;
public function Border(x:int,y:int,width:int,height:int)
{
_x = x;
_y = y;
_w = width;
_h = height;
drawBorder();
}
public function drawBorder():void
{
//定义边框颜色
var borderColor:uint;
var borderColorDrk1:uint
var borderColorDrk2:uint
var borderColorLt1:uint	
var borderInnerColor:uint;
//设定边框颜色
borderColor = 0xb7babc;		
borderColorDrk1 =
ColorUtil.adjustBrightness2(borderColor, -40);
borderColorDrk2 =
ColorUtil.adjustBrightness2(borderColor, +25);
borderColorLt1 = 
ColorUtil.adjustBrightness2(borderColor, +40);				
borderInnerColor = 0xffffff;
//画出3D边框效果
draw3dBorder(borderColorDrk2, borderColorDrk1, borderColorLt1,
Number(borderInnerColor), 
Number(borderInnerColor), 
Number(borderInnerColor));
}
public function draw3dBorder(c1:Number, c2:Number, c3:Number,
c4:Number, c5:Number, c6:Number):void
{
var g:Graphics = graphics;
g.clear();
// outside sides
g.beginFill(c1);
g.drawRect(_x, _y, _w, _h);
g.drawRect(_x+1, _y, _w - 2, _h);
g.endFill();
// outside top
g.beginFill(c2);
g.drawRect(_x+1, _y, _w - 2, 1);
g.endFill();
// outside bottom
g.beginFill(c3);
g.drawRect(_x+1, _y + _h - 1, _w - 2, 1);
g.endFill();
// inside top
g.beginFill(c4);
g.drawRect(_x+1, _y+1, _w - 2, 1);
g.endFill();
// inside bottom
g.beginFill(c5);
g.drawRect(_x+1, _y + _h - 2, _w - 2, 1);
g.endFill();
// inside sides
g.beginFill(c6);
g.drawRect(_x+1, _y+2, _w - 2, _h - 4);
g.drawRect(_x+2, _y+2, _w - 4, _h - 4);
g.endFill();
}
}
}

 

FocusRect.as

package skins
{
import flash.display.Graphics;
import flash.display.Shape;
/**
* 该代码改编自mx.skins.halo.HaloFocusRect
*/
public class FocusRect extends Shape
{
public function FocusRect(x:int,y:int,w:int,h:int)
{
super();
//设置画图要素
var focusBlendMode:String = "normal"; //getStyle("focusBlendMode");
var focusAlpha:Number = 0.4;          //getStyle("focusAlpha");
var focusColor:Number = 0x009dff;     //getStyle("focusColor");
var cornerRadius:Number = 0;          //getStyle("cornerRadius");
var focusThickness:Number = 2;        //getStyle("focusThickness");
var focusRoundedCorners:String = "tl tr bl br";  //getStyle("focusRoundedCorners");
var themeColor:Number = 0x009dff;     //getStyle("themeColor");			
var rectColor:Number = focusColor;
//画出代表焦点的矩形	
var g:Graphics = graphics;
g.clear();
var ellipseSize:Number;
// outer ring
g.beginFill(rectColor, focusAlpha);
ellipseSize = (cornerRadius > 0 ? cornerRadius + focusThickness : 0) * 2;
g.drawRoundRect(x, y, w, h, ellipseSize, ellipseSize);
ellipseSize = cornerRadius * 2;
g.drawRoundRect(x+focusThickness, y+focusThickness,
w - 2 * focusThickness, h - 2 * focusThickness,
ellipseSize, ellipseSize);
g.endFill();
// inner ring
g.beginFill(rectColor, focusAlpha);
ellipseSize = (cornerRadius > 0 ? cornerRadius + focusThickness / 2 : 0) * 2;
g.drawRoundRect(x+focusThickness / 2, y+focusThickness / 2,
w - focusThickness, h - focusThickness,
ellipseSize, ellipseSize);
ellipseSize = cornerRadius * 2;
g.drawRoundRect(x+focusThickness, y+focusThickness,
w - 2 * focusThickness, h - 2 * focusThickness,
ellipseSize, ellipseSize);
g.endFill();
}
}
}


效果图:


 

这篇关于Flex中的TextInput (5):添加焦点效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/872507

相关文章

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤

《SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤》本文主要介绍了SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤,文中通过示例代码介绍的非常详... 目录 目标 步骤 1:确保 ProxySQL 和 mysql 主从同步已正确配置ProxySQL 的

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

C#实现WinForm控件焦点的获取与失去

《C#实现WinForm控件焦点的获取与失去》在一个数据输入表单中,当用户从一个文本框切换到另一个文本框时,需要准确地判断焦点的转移,以便进行数据验证、提示信息显示等操作,本文将探讨Winform控件... 目录前言获取焦点改变TabIndex属性值调用Focus方法失去焦点总结最后前言在一个数据输入表单

基于Python实现PDF动画翻页效果的阅读器

《基于Python实现PDF动画翻页效果的阅读器》在这篇博客中,我们将深入分析一个基于wxPython实现的PDF阅读器程序,该程序支持加载PDF文件并显示页面内容,同时支持页面切换动画效果,文中有详... 目录全部代码代码结构初始化 UI 界面加载 PDF 文件显示 PDF 页面页面切换动画运行效果总结主

React实现原生APP切换效果

《React实现原生APP切换效果》最近需要使用Hybrid的方式开发一个APP,交互和原生APP相似并且需要IM通信,本文给大家介绍了使用React实现原生APP切换效果,文中通过代码示例讲解的非常... 目录背景需求概览技术栈实现步骤根据 react-router-dom 文档配置好路由添加过渡动画使用

使用Python实现生命之轮Wheel of life效果

《使用Python实现生命之轮Wheeloflife效果》生命之轮Wheeloflife这一概念最初由SuccessMotivation®Institute,Inc.的创始人PaulJ.Meyer... 最近看一个生命之轮的视频,让我们珍惜时间,因为一生是有限的。使用python创建生命倒计时图表,珍惜时间

防近视护眼台灯什么牌子好?五款防近视效果好的护眼台灯推荐

在家里,灯具是属于离不开的家具,每个大大小小的地方都需要的照亮,所以一盏好灯是必不可少的,每个发挥着作用。而护眼台灯就起了一个保护眼睛,预防近视的作用。可以保护我们在学习,阅读的时候提供一个合适的光线环境,保护我们的眼睛。防近视护眼台灯什么牌子好?那我们怎么选择一个优秀的护眼台灯也是很重要,才能起到最大的护眼效果。下面五款防近视效果好的护眼台灯推荐: 一:六个推荐防近视效果好的护眼台灯的