运行效果:
颜色选择器

点击色块后可复制颜色值

复制颜色值

整个程序只用到了一个控件,适配器中的"GridView"。

布局代码(layout.aly):

{
  LinearLayout;
  layout_width="-1";
  BackgroundColor="0xff000000";
  orientation="vertical";
  layout_height="-1";
  {
    GridView;
    numColumns=3;
    id="cg";
    layout_width="-1";
    verticalSpacing="1dp";
    horizontalSpacing="1dp";
    layout_height="-1";
  };
};

以上代码在linearLayout布局中创建一个列数为3的表格。

下面是主逻辑代码(main.lua):

require"import"
import "android.app.*"
import "android.os.*"
import "android.widget.*"
import "android.view.*"
import "android.content.Context" 
activity.setTitle("Color Picker")
--activity.setTheme(R.Theme_Google)
activity.setContentView(loadlayout"layout")

item={
  TextView,
  id="cl",
  paddingTop="30",
  paddingBottom="30",
  text="h",
  layout_width="fill",
  BackgroundColor="0xff000000",
  TextColor="0xffffffff",
  Gravity="center"
}
--创建项目数组
data={}
--创建适配器
adp=LuaAdapter(activity,data,item)
--添加数据
cls={
  0xffECEFF1,
  0xffB0BEC5,
  0xff78909C,
  0xff546E7A,
  0xffEFEBE9,
  0xffBCAAA4,
  0xff8D6E63,
  0xff6D4C41,
  0xffFBE9E7,
  0xffFFAB91,
  0xffFF7043,
  0xffF4511E,
  0xffFFF3E0,
  0xffFFCC80,
  0xffFFA726,
  0xffFB8C00,
  0xffFFF8E1,
  0xffFFE082,
  0xffFFCA28,
  0xffFFB300,
  0xffFFFDE7,
  0xffFFF59D,
  0xffFFEE58,
  0xffFDD835,
  0xffF9FBE7,
  0xffE6EE9C,
  0xffD4E157,
  0xffC0CA33,
  0xffF1F8E9,
  0xffC5E1A5,
  0xff9CCC65,
  0xff7CB342,
  0xffE8F5E9,
  0xffA5D6A7,
  0xff66BB6A,
  0xff43A047,
  0xffE0F2F1,
  0xff80CBC4,
  0xff26A69A,
  0xff00897B,
  0xffE0F7FA,
  0xff80DEEA,
  0xff26C6DA,
  0xff00ACC1,
  0xffE1F5FE,
  0xff81D4FA,
  0xff29B6F6,
  0xff039BE5,
  0xffE3F2FD,
  0xff90CAF9,
  0xff42A5F5,
  0xff1E88E5,
  0xffE8EAF6,
  0xff9FA8DA,
  0xff5C6BC0,
  0xff3949AB,
  0xffEDE7F6,
  0xffB39DDB,
  0xff7E57C2,
  0xff5E35B1,
  0xffF3E5F5,
  0xffCE93D8,
  0xffAB47BC,
  0xff8E24AA,
  0xffFCE4EC,
  0xffF48FB1,
  0xffEC407A,
  0xffD81B60,
  0xffFFEBEE,
  0xffEF9A9A,
  0xffEF5350,
  0xffE53935
}
cs={
  "#ECEFF1",
  "#B0BEC5",
  "#78909C",
  "#546E7A",
  "#EFEBE9",
  "#BCAAA4",
  "#8D6E63",
  "#6D4C41",
  "#FBE9E7",
  "#FFAB91",
  "#FF7043",
  "#F4511E",
  "#FFF3E0",
  "#FFCC80",
  "#FFA726",
  "#FB8C00",
  "#FFF8E1",
  "#FFE082",
  "#FFCA28",
  "#FFB300",
  "#FFFDE7",
  "#FFF59D",
  "#FFEE58",
  "#FDD835",
  "#F9FBE7",
  "#E6EE9C",
  "#D4E157",
  "#C0CA33",
  "#F1F8E9",
  "#C5E1A5",
  "#9CCC65",
  "#7CB342",
  "#E8F5E9",
  "#A5D6A7",
  "#66BB6A",
  "#43A047",
  "#E0F2F1",
  "#80CBC4",
  "#26A69A",
  "#00897B",
  "#E0F7FA",
  "#80DEEA",
  "#26C6DA",
  "#00ACC1",
  "#E1F5FE",
  "#81D4FA",
  "#29B6F6",
  "#039BE5",
  "#E3F2FD",
  "#90CAF9",
  "#42A5F5",
  "#1E88E5",
  "#E8EAF6",
  "#9FA8DA",
  "#5C6BC0",
  "#3949AB",
  "#EDE7F6",
  "#B39DDB",
  "#7E57C2",
  "#5E35B1",
  "#F3E5F5",
  "#CE93D8",
  "#AB47BC",
  "#8E24AA",
  "#FCE4EC",
  "#F48FB1",
  "#EC407A",
  "#D81B60",
  "#FFEBEE",
  "#EF9A9A",
  "#EF5350",
  "#E53935"
}
for i,v in pairs(cls)do
  if i%4~=1 then
    vv=string.gsub(v,"0xff","#",1)
    table.insert(data,{
      cl={
        BackgroundColor=v,
        text=collectgarbages[i]
      }, 
    })
  end
end

cg.Adapter=adp

cg.onItemClick=function(l,v,p,i)
  k=v.text
  activity.getSystemService(Context.CLIPBOARD_SERVICE).setText(k) 
  print(k,"已复制到剪切板")
end

控件背景颜色值需要是二进制的,然后复制时得到的文本是常规的rgb颜色值,你可以不用像我这样写两个数组,只写一个也是可以的,然后用匹配字符串来切割替换。

标签: Lua

已有 3 条评论

  1. 飒飒 飒飒

    6666

  2. 这个很实用 可以引用了

  3. 大佬越来越厉害,都看不懂写的啥了我

添加新评论