wangy 发表于 2021-7-1 08:46:20

【lua教程】编辑框封装

--编辑框封装
--Pretend
require "import"
import "android.app.*"
import "android.os.*"
import "android.widget.*"
import "android.view.*"

function PretendEditText(t)--编辑框封装
local lay=loadlayout{FrameLayout,
    {
      RelativeLayout;
      layout_width="fill";
      focusableInTouchMode=true;
      focusable=true;
      layout_height="fill";
      {
      EditText;
      layout_height="wrap";
      layout_marginTop="56";
      layout_centerHorizontal="true";
      textColor=t.字体颜色 or "#000000";
      textSize="14dp";
      background="0";
      layout_marginLeft="16dp";
      layout_width="fill";
      layout_marginRight="16dp";
      id=t.id;
      password=t.password;
      singleLine="true";
      };


      {
      TextView;
      layout_height="2dp";
      layout_alignBottom=t.id;
      layout_centerHorizontal="true";
      background=t.线的颜色1 or "#009688";
      layout_marginLeft="16dp";
      layout_width="fill";
      layout_marginRight="16dp";
      alpha="1";
      id=t.id2;
      };

      {
      TextView;
      layout_height="1dp";
      layout_alignBottom=t.id;
      layout_centerHorizontal="true";
      background=t.线的颜色2 or "#000000";
      layout_marginLeft="16dp";
      layout_width="fill";
      layout_marginRight="16dp";
      alpha="0.2";
      id=t.id4;
      };

      {
      TextView;
      id=t.id3;
      layout_marginLeft="24dp";
      text=t.hint or "Pretend封装";
      layout_alignBaseline=t.id;
      textSize="14dp";
      };
    };}

值=true

lay.getChildAt(0).getChildAt(0).setOnFocusChangeListener{
    onFocusChange=function( v, hasFocus)
      if hasFocus then

      import "android.view.animation.ScaleAnimation"
      lay.getChildAt(0).getChildAt(1).startAnimation(ScaleAnimation(0.0,1.0,1.0,1.0,1,0.5,1,0.5).setDuration(200))
      lay.getChildAt(0).getChildAt(1).setVisibility(View.VISIBLE)
      if lay.getChildAt(0).getChildAt(0).Text=="" then
          import "android.view.animation.TranslateAnimation"
          lay.getChildAt(0).getChildAt(3).startAnimation(TranslateAnimation(0,0,0,-56).setDuration(100).setFillAfter(true))
      end

       else

      lay.getChildAt(0).getChildAt(1).startAnimation(ScaleAnimation(1.0,0.0,1.0,1.0,1,0.5,1,0.5).setDuration(200))
      lay.getChildAt(0).getChildAt(1).setVisibility(View.INVISIBLE)
      if #lay.getChildAt(0).getChildAt(0).Text==0 then
          lay.getChildAt(0).getChildAt(3).startAnimation(TranslateAnimation(0,0,-56,0).setDuration(100).setFillAfter(true))
         else
          lay.getChildAt(0).getChildAt(3).setTextColor(t.hint颜色1 or 0xff009688)
      end

      end
    end}
lay.getChildAt(0).getChildAt(0).addTextChangedListener{
    onTextChanged=function(s)
      内容=tostring(lay.getChildAt(0).getChildAt(0).Text)
      if #内容==0 then
      lay.getChildAt(0).getChildAt(3).setTextColor(t.hint颜色2 or 0xff8b8b8b)
       else
      lay.getChildAt(0).getChildAt(3).setTextColor(t.hint颜色1 or 0xff009688)
      end
    end
}


lay.getChildAt(0).getChildAt(1).setVisibility(View.INVISIBLE)

function 取消焦点(app)
    app.setFocusable(false);
    app.setFocusableInTouchMode(true);
end

return function() return lay end
end

--主布局,导入封装模块必须在布局前面
layout={
LinearLayout;
gravity="center";
orientation="vertical";
{
    PretendEditText
    {
      id="app";
      hint="账号";
      hint颜色1=0xFFE91E63;
      hint颜色2=0xff8b8b8b;
      字体颜色="#000000";
      线的颜色1="#FFE91E63";
      线的颜色2="#ff8b8b8b";
    };
};
{
    PretendEditText
    {
      id="ap47p";

      hint="密码";
      password=true;
      字体颜色="#000000";
      hint颜色1=0xFFE91E63;
      hint颜色2=0xff8b8b8b;
      线的颜色1="#FFE91E63";
      线的颜色2="#ff8b8b8b";
    };
};
{
    Button;
    id="apo";
    layout_marginLeft="10dp";
    layout_width="fill";
    text="确定";
    layout_marginRight="10dp";
    layout_marginTop="56";
};
};

activity.setContentView(loadlayout((layout)))

function apo.onClick()
取消焦点(app)
取消焦点(ap47p)
end

页: [1]
查看完整版本: 【lua教程】编辑框封装