登录  | 立即注册

游客您好!登录后享受更多精彩

查看: 1513|回复: 2

[lua教程] 【lua教程】简易计算器

[复制链接]

444

主题

509

帖子

2051

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2051

荣誉管理论坛元老

发表于 2021-7-1 08:41:58 来自手机 | 显示全部楼层 |阅读模式 来自:
  1. require "import"* O% R3 I. `; o; g6 Y( r5 ^
  2. import "android.widget.*"' ], H6 m8 j; H* g
  3. import "android.view.*"- T- E1 j3 `. J1 z
  4. import "android.text.method.*"
    $ x& z8 [* T0 }1 ]
  5. import 'android.webkit.WebView'* a% |& D' A5 E9 e& F
  6. import "android.app.AlertDialog"% Y# v# z( x1 w* Z" a# C
  7. local hh={}6 @$ ^+ D- y0 ~! g. o7 m
  8. 4 g0 |4 q  n* G. O& D0 {7 h* r
  9. layout={
    . Y2 z, W3 U4 v9 K% k; {- j% X. k
  10.   LinearLayout,
    & L! h2 ]) r: o* ~
  11.   layout_width="fill",
    ( S+ X- |! b7 I5 {* Q* F
  12.   layout_height="fill",
    2 R8 J  x9 r7 J( ?# @  a8 J5 j
  13.   orientation="vertical",
    * L) u8 V$ t' \  I
  14.   background='#ffe9e9e9',
    2 l, L* p: D" S2 O, B7 ^
  15.   {1 B* P6 Q1 d5 T$ U3 j, G& z/ e
  16.     HorizontalScrollView,
    ; W% I6 p, x( \6 k& F3 ~
  17.     FillViewport=true,6 ?, q1 T' A# G2 k/ O1 [
  18.     layout_gravity="left",6 R, `, _4 e+ D  E- V. F; u' B
  19.     layout_width="fill",1 ^+ l; X+ X; i# Y( o/ i
  20.     {. n2 ~9 P, D7 q9 P6 i4 N
  21.       TextView,7 c0 g* ]0 [% l8 \: f2 i
  22.       text="",
    , b& H- m0 K* _, m% y9 H' C* W
  23.       textSize=30,
    4 V/ ^/ r9 U1 d( d
  24.       textColor='0xff000000',+ t' ^( l& h9 t$ d$ u2 J
  25. & w! R6 p- u- B  ?  [" F# @
  26.       lines=1,3 \# b6 h, t) D2 y
  27.       Gravity="left",/ w+ t% @) d7 Z+ n" ?
  28.       layout_width="wrap",: j9 d: n" A) ^  R! f- g) J
  29.     },2 }0 T1 T- O* E+ H# C# w2 g4 F) b
  30.   },
    3 Y1 [* u, t( F
  31.   {
    7 Q3 {- M8 R2 N" e
  32.     HorizontalScrollView,* E( i- X0 [3 t9 U. p7 g6 A
  33.     FillViewport=true,
    . A& T# w+ c  _( u( B& K# {
  34.     layout_gravity="left",% G9 v9 R, ^& J* S4 H
  35.     layout_width="fill",
    2 F6 P* ^  c! E! c/ v
  36.     {
    ; h2 b" N0 ~$ ^6 D( F
  37.       TextView,
    # d' O  g- b6 {9 C$ G4 R8 A: g
  38.       text="",
    1 B5 B( ~( Z) z
  39.       id="input",
      g8 n2 M( a$ e. t. R+ D
  40.       textSize=60,4 g) k0 d% u3 R
  41.       textColor='0xff000000',% S, R9 b+ W7 [: t0 |$ R  l
  42.       lines=1,/ `. F5 G6 s1 v+ h8 k& s6 R
  43.       Gravity="left",0 m9 y! @7 n: n3 \
  44.       layout_width="wrap",
    : z& [" J: H/ l& R, I5 j* T
  45.     },
    - j! T6 {' R' ~. d
  46.   },. g; s# [0 ^! a, \, Q) |5 s
  47.   {# M! [) {2 y0 n" d2 X1 {$ Y
  48.     HorizontalScrollView,
    ! V% A  v" U2 z, Y( |1 [
  49.     FillViewport=true,% o7 [& l4 {5 o8 ?( k! m/ ?; i
  50.     layout_gravity="right",$ `9 `, }: Y& u0 W; g- z
  51.     layout_width="fill",
    : a: ?9 U/ p6 i$ V5 P
  52.     {
    . I0 v! u+ B& A* O& a' ~
  53.       TextView,
    6 z1 C; X4 z5 {5 }
  54.       text="",
    , e( z0 j" u, ~8 J6 N
  55.       id="output",
    ' e* t. z, G8 c0 S+ |; M
  56.       Gravity="right"," L2 `3 ~& `4 g
  57.       textSize=80,
    : k0 Z6 C6 @- h7 ^2 H6 i# k
  58.       textColor='0xff000000',
    . g0 j6 ^/ O3 h! U! r% X- z
  59.       onClick="up",
    $ ~2 C# p" E0 B9 `) ]! R" w
  60.       lines=1,# L6 f) M) C( j- @
  61.       layout_width="wrap",; [, y0 ]* b1 ?8 {& Y
  62.     },
    3 }* q+ ~! p3 I" Q8 c  I8 n
  63.   },
    7 C, b0 Z6 ~) j+ R8 l- e0 z, O
  64.   {" }8 {: I$ A$ I! D6 A
  65.     LinearLayout,  G) u, N. n9 q/ U
  66.     layout_width="fill",% g4 d; M6 b' |1 J
  67.     layout_height="fill",9 ?( o- d4 i- o0 f
  68.     layout_weight="1.0",
    ' }; x4 N9 j* ?) V/ p: k; n
  69.     {
    - B) q( l4 |0 ]
  70.       Button," x/ V( c2 M! R, q! |
  71.       textSize=40,. V, ?  B8 g. ~
  72.       textSize=40,; c6 ~) b" K5 f1 ^9 R
  73.       text="(",) V" @5 F# d7 Z7 ^" k
  74.       onClick="click",
    & ^7 O; J  a! O1 r6 s
  75.       background='#fffafafa',
    % ?, c. x% ~: n& }/ j9 J
  76.       layout_width="fill",
    0 x8 ?( H4 P! R1 ~( m9 y9 ]
  77.       layout_height="fill",
    ! K+ @* E1 P" [' S% j! S
  78.       layout_weight="1.0",
    & h! v' h' L# j; v/ q, _
  79.     },
    6 u% Z: X# i" P7 H) c
  80.     {: W  Y5 b+ w% w8 c4 i6 q+ E
  81.       Button,
    . \- ^* ?0 E0 R+ g
  82.       textSize=40,. O7 X" K. {4 f1 A" {& A3 J
  83.       text=")",
    1 @1 q1 R# M9 Q, D
  84.       onClick="click",
    , J4 q: s! r9 S, d4 Y8 P
  85.       layout_width="fill",3 s8 Q( `+ _, V+ x, }, m
  86.       background='#fffafafa',2 X! ]: @  [+ ^% Z! k
  87.       layout_height="fill",- P; v" F) n. E+ i3 i
  88.       layout_weight="1.0",
    ' a/ G2 f9 H# X0 ~- ~' f9 Q
  89.     },+ I+ o0 I/ r0 Y% K$ x) ]% F7 T
  90.     {
    / }* O( V4 x2 X
  91.       Button,
    0 P, p" ], W$ s0 |7 J' E: i
  92.       textSize=30,
    , M5 o& d5 }+ O& D/ S
  93.       text="菜单",( p8 N, A6 G, `
  94.       id="endg",
    9 ~) D9 t1 p* B* i
  95.       onClick="菜单",
    8 N  ^; X. K( `; ]
  96.       background='#fffafafa',
    9 J5 W. i1 y4 X7 t9 K
  97.       layout_width="fill",
    & Z8 u* Y8 n2 K) b! l0 v; h
  98.       layout_height="fill",
    - K$ o7 ?/ @! D+ I+ `
  99.       layout_weight="1.0",$ H9 ~0 O' y: a9 f: v
  100.     },6 R8 \7 j+ \# r
  101.     {
      Q) @3 n; Z, A+ Z
  102.       Button,
    4 X. D* ?4 O5 u" W7 H6 _# f3 ?
  103.       textSize=30," D% F+ q. G5 Z% B0 L
  104.       text="清空",
    1 s; `* R7 L9 K* g0 W
  105.       onClick="clear",
    ' S6 s( t& u- W  g" o* O
  106.       background='#fffafafa',
    ) p/ _$ D9 Z4 ?/ x) k- Y# j
  107.       layout_width="fill",
    & S9 s+ C& r% A7 Z5 [9 h# b
  108.       layout_height="fill",
    0 f" ]! H" i  `, F
  109.       layout_weight="1.0",
    9 ]2 ?  Q) J9 W5 M: p( w( z! w
  110.     },) `: Y3 H8 T2 g8 w3 \8 R) s; P8 p
  111.   },
    0 ?4 z' R$ z1 R* x1 T1 j
  112.   {
    9 c; @2 D( g* r. H6 u
  113.     LinearLayout,. ~3 h' i: q6 i
  114.     layout_width="fill",3 k3 A9 S) x1 u  b
  115.     layout_height="fill",4 z5 A: ~- t3 _2 C) N
  116.     layout_weight="1.0",2 Y" g+ F2 B6 W0 Q- o
  117.     {
    + c. b- c. q) X9 x' m0 b
  118.       Button,
    4 M( D# @5 P" G" A4 l
  119.       textSize=40,+ ?, b1 l- Y; h: [
  120.       text="1",3 C- r5 D0 u! J- X7 g
  121.       onClick="click",
    7 a- m* K2 x9 q+ ~/ @
  122.       backgroundColor='#ffffffff',
    7 o3 z* u4 F8 N2 F) |; w
  123.       layout_width="fill",7 C# A' Z2 d, I- Q. S, G
  124.       layout_height="fill",: V+ U! y* A( u4 |
  125.       layout_weight="1.0",
    5 n% x. [1 N3 \# ^! P* B% P/ Q
  126.     },8 `# ]2 O8 K& u5 ~
  127.     {7 R+ [, }# U8 f6 b/ H
  128.       Button,
    " v, F- k$ l3 P' U! s: F
  129.       textSize=40,
    , B* l. |, ~0 J
  130.       text="2",
    1 O' X, e! R2 D! \. ^
  131.       onClick="click",8 w, [8 a- k" T% V1 k7 J) m9 A# I
  132.       backgroundColor='#ffffffff',3 T) k  L$ O' _; {8 i$ }% }. E
  133.       layout_width="fill",
    ; a" P& c) i8 @. S
  134.       layout_height="fill",
    1 i; T6 x& x  U% t
  135.       layout_weight="1.0",
    ! u+ y6 F8 e" _% P
  136.     },4 N: T, \, \4 i
  137.     {
    % M$ l2 Q+ @8 d. W
  138.       Button,
    ' W* J' D# C, E1 ~/ Z
  139.       textSize=40,9 T" t( d8 Z9 b# S  g8 s
  140.       text="3",
    6 b/ u. ~2 x5 Z' l. u7 `5 @
  141.       onClick="click",3 f' s6 |3 K9 d9 {8 r" c7 ]* v. T
  142.       backgroundColor='#ffffffff',9 _2 r! }7 K, y5 [* E+ j& i
  143.       layout_width="fill",
    ! m9 E4 P+ c% b- z- |
  144.       layout_height="fill",
    2 {9 a$ \6 V5 N/ }) o0 F* V/ S
  145.       layout_weight="1.0",2 i' O, O; v$ n6 G6 u" h) c: i
  146.     },- @3 n5 G5 C  v' t6 L+ j2 M
  147.     {/ W- L: p5 ~/ [  f
  148.       Button,
      c; b# i! \/ v/ s5 E
  149.       textSize=40,8 G; J4 J4 ]: n3 O8 `
  150.       text="+",& L, ^- A3 e' V- h
  151.       onClick="click"," d3 p) m+ _/ }3 ^/ p+ Y# @3 t
  152.       layout_width="fill",$ p( _5 R  k1 M1 k
  153.       background='#fffafafa',
    / Z5 ?# b6 Z+ e/ r
  154.       layout_height="fill",  Y- ^% J; ^' `0 L7 \% }0 K- \
  155.       layout_weight="1.0",8 F6 [  a3 B$ Y& Z! m
  156.     },$ [5 \' O% e/ o% x
  157.   },0 B2 b& M0 I9 D* o7 A( w0 \
  158.   {( u7 l5 x. {) d3 [2 V
  159.     LinearLayout,- {; ]$ k/ d1 b' u  I8 X
  160.     layout_width="fill",( ~2 q, x% j5 D- Y# D. J) T1 e# l
  161.     layout_height="fill",
    ' ^/ E" T- X4 U# E4 m( L8 F. V
  162.     layout_weight="1.0",
    8 W/ i* d5 B2 U
  163.     {
    ; i3 R& _6 |7 \5 E! `( M: m/ g
  164.       Button,7 v% _+ h7 j/ {- u; R% t
  165.       textSize=40,. R& z5 q8 |+ i7 ?+ m8 j
  166.       text="4",
    : a* A' Z4 N& M( @+ T
  167.       onClick="click",
    ) E7 h+ |& q' l
  168.       backgroundColor='#ffffffff',
    9 o& b' ]5 f3 y* U9 g$ y- b/ l, S
  169.       layout_width="fill",
    2 A* d* N# `6 r
  170.       layout_height="fill",
    " a' r. {7 S- T9 {3 ~
  171.       layout_weight="1.0",
    / K$ E7 z- Z# J) s4 c* B
  172.     },
    , X4 v6 q; q/ F, @& j$ v8 d
  173.     {
    & _! C* o: s' u
  174.       Button,
    1 ?: a- `) A, n" s8 y2 d! U6 S
  175.       textSize=40,9 X, m3 y# r& h* b. B
  176.       text="5",
    $ R4 o+ t- B; v
  177.       onClick="click",0 I: l9 ]5 z  p5 M, R, L
  178.       backgroundColor='#ffffffff',
    7 B  S7 F  A1 W: j3 r5 Z# @
  179.       layout_width="fill",
    ) V" O$ W* L9 b8 U/ Q: {! c
  180.       layout_height="fill",
    , O( R0 U, ]4 Y% E+ s4 R5 Y( {
  181.       layout_weight="1.0",
    0 H. s& T6 n& C. A% _# ~5 B
  182.     },
      F$ n! P1 f9 U( ^7 n
  183.     {) Z5 H! f: w  f0 E3 ?1 t# @
  184.       Button,2 _* t8 Y% e6 G9 I& q# J5 L$ x$ W
  185.       textSize=40,
    1 K2 V) N. K5 z$ x
  186.       text="6",% i! @. S! @* b4 g
  187.       backgroundColor='#ffffffff',+ C( L+ y# ?6 \9 V
  188.       onClick="click",, j) T: T- p2 S: `3 e- G
  189.       layout_width="fill",' O0 A0 B/ m) D7 ]8 Q/ b
  190.       layout_height="fill",
    6 J/ Z& U* `" z
  191.       layout_weight="1.0",7 P. Q' M, p0 O, R9 J3 ]' W
  192.     },
    ( h* Q  b! d$ r  F
  193.     {) f' Y" ]' D% S# J7 E
  194.       Button,. B  Z. }4 r+ }
  195.       textSize=40,
    1 d% m6 I$ z7 {, Y- E. @3 l4 i
  196.       text="-",& Q2 b+ Z, z1 |# u
  197.       onClick="click",5 G/ @4 E' e; R# ]
  198.       layout_width="fill",
    7 I) W7 {8 k, r. S
  199.       layout_height="fill",  |" h. {, I% d8 ?; _
  200.       background='#fffafafa',5 K' ?* P9 f) K) a; ]0 }2 ~9 O9 d
  201.       layout_weight="1.0"," E5 v# t; P3 Q4 O0 ]
  202.     },
    1 l' G: I# F" f) S
  203.   },7 p, y. x) s3 R  U$ j4 A3 A
  204.   {8 q& I+ b" |# }$ n/ p: R! Q. P0 |) n
  205.     LinearLayout,
    2 |/ u' C) B4 {5 Z' |
  206.     layout_width="fill"," `' H( C" R# l9 h& `
  207.     layout_height="fill",3 B% o! l5 `8 s9 ?1 r% t8 P
  208.     layout_weight="1.0",
      {# N7 f  J7 d, j
  209.     {
    ; m: ^9 y4 U, F9 Y9 l4 H
  210.       Button,
    # S2 F; J/ W0 l! t" ~& S$ P
  211.       textSize=40,
    . O. R) y: ?) R. `/ T
  212.       text="7",( `' W4 B" x+ L
  213.       onClick="click",
    2 X0 K/ O  r6 J
  214.       backgroundColor='#ffffffff',
      t) B% l) D! d6 H
  215.       layout_width="fill",4 G1 ]7 r* D3 N9 W8 J! N/ C
  216.       layout_height="fill",% e. S  y& u6 O
  217.       layout_weight="1.0",, h1 `1 d; A+ o( v
  218.     },
    4 ~+ S6 o: [$ {1 R- h
  219.     {
    0 k1 f- V' l. P  J4 n+ O
  220.       Button,! T! N8 F" H& S. ]
  221.       textSize=40,. O) [0 m' K4 y) ?
  222.       text="8",! `& m' ^) T$ r4 [. T1 `6 H# Q
  223.       onClick="click",
    ! R5 ^/ `% S$ p! V' u. w  r1 X
  224.       backgroundColor='#ffffffff',
    1 a' p( b- Q* Z! y! k! ~
  225.       layout_width="fill",' [1 _6 _0 G# ?( i4 d& u5 h# Z
  226.       layout_height="fill",
    ; a2 p/ D, a% i, n4 [
  227.       layout_weight="1.0",4 @/ K: B1 W# o% \/ `
  228.     },$ _, w/ L8 _8 H- n9 s
  229.     {# U. Y& U/ `4 O. u2 H0 y
  230.       Button,7 q+ S+ i9 Z6 C. _7 D0 k
  231.       textSize=40,' _9 ?7 @$ W! s3 V% z4 r- t# a3 e
  232.       text="9",3 l" r  V$ t7 B0 n
  233.       onClick="click",
    - k6 M5 Z$ S' E0 n7 D
  234.       backgroundColor='#ffffffff',) [$ j0 q& }! D( C* i
  235.       layout_width="fill",
    - f5 R' p2 p& K' |7 R, v8 b: B; W& _
  236.       layout_height="fill",
    # }7 n. y5 p$ o4 _% K" A
  237.       layout_weight="1.0",
    ! d0 t" O. i4 I( \% B6 R6 J3 ^
  238.     },
    0 _9 N4 v! w! N! y8 q
  239.     {0 b+ J4 Y  `/ m" b) v9 L- g
  240.       Button,
    ' E" S8 T" }! w5 s% u
  241.       textSize=40,; ~" x0 L2 Q, ?8 D3 E9 z+ S+ J0 H
  242.       text="*",( `3 D/ Y& c- `; x" v+ x
  243.       onClick="click",; J+ g* P6 T0 ?. U8 w, W# n7 S* `$ z6 l
  244.       layout_width="fill",4 G6 b; q4 d  S
  245.       layout_height="fill",  F% r4 _: ]8 _. l( f# ^
  246.       background='#fffafafa',) Y( l; a! |! m  R  g0 F
  247.       layout_weight="1.0",$ V/ k" Q, K3 v6 b  v
  248.     },9 I& H2 ^' s3 e5 o$ w4 x
  249.   },
    & l$ H6 B  R$ @+ G0 Q
  250.   {% Z5 m( G- H. F* E  b5 [5 x4 d0 k* V
  251.     LinearLayout,, n( K3 Q! I) E+ L
  252.     layout_width="fill",
    - s9 l( y& D, t- Q
  253.     layout_height="fill",
    5 r, d. \& I/ @: d! M$ G
  254.     layout_weight="1.0",$ E  N' B! N" _3 i; T9 D
  255.     {
    . y6 v" F/ R  @9 U7 Z9 @, x' a
  256.       Button,
    ; o* n$ d: |9 b/ r0 i0 B9 b9 C
  257.       textSize=40,6 _. y- g' F$ H2 M' |
  258.       text="0",3 k# a* w8 J' z1 n  u# K
  259.       onClick="click",
    6 H6 T# }' k% \7 |" S/ y
  260.       backgroundColor='#ffffffff',9 _" ~% q6 Q  R/ W
  261.       layout_width="fill",% V  j. d# J  o, }$ C, g* B
  262.       layout_height="fill",7 m3 R2 Z" o7 c% ?5 g
  263.       layout_weight="1.0",1 C; H0 o% \. F' ~5 R
  264.     },
    ) l* Q3 n; r; k* m! t0 d, a/ @0 M
  265.     {
    # l; v, a7 l* z4 {0 Z$ Z- l# I
  266.       Button,
    / n7 J* q4 G& q* Q! ]
  267.       textSize=40,1 I; k" k- d1 U: H
  268.       text=".",; L! E# ]9 M" u: y1 b
  269.       onClick="click",
    - L1 O) e( q+ J! c
  270.       layout_width="fill",$ l- Q" A; z7 p$ v9 }4 ?* ^; |
  271.       layout_height="fill",
    # r$ x7 n& i7 W, E! F- ?2 N
  272.       background='#fffafafa',  S: \% p+ V( o, }1 W% O
  273.       layout_weight="1.0",) k5 Q5 R& x6 j1 }) z& ~
  274.     },' z. l* M8 L$ L) }# e" T
  275.     {* F0 A5 Z! N7 n! ]2 s
  276.       Button,
    2 p( o# `- ]0 `5 _5 l) y) ]% E  s
  277.       textSize=40,/ d3 h, L# Y! i0 J
  278.       text="=",# x& ~+ n8 u6 u* i
  279.       textColor="#FFffffff",
    7 a) U% ]: ]% r) k' \0 x
  280.       onClick="enter",
    $ P4 Y8 V+ X, j5 R8 P5 o
  281.       layout_width="fill",3 A, ^  s0 X4 i
  282.       layout_height="fill",: q8 J9 |. W5 {6 m* v& R& f5 c1 t
  283.       background='#FF006BFF',) u) s0 ^/ `; }8 B
  284.       layout_weight="1.0",' n* m2 z, u9 Y* a7 G7 y) o
  285.     },
    0 Z) s  T9 L& W5 G6 M0 E9 s
  286.     {3 H' Q- i+ N- Z) P
  287.       Button,
    2 P$ T' Z( g, }5 y! i- Z
  288.       textSize=40,4 e1 J+ l* s3 K$ W
  289.       text="/",
    ' V& R8 s( x9 ~3 g9 H  N1 f; ?' F
  290.       onClick="click",
    8 N7 G/ i0 u7 ?# ~' V3 A: Z1 D
  291.       layout_width="fill",, E4 n6 m0 Y4 V% B2 g9 I4 H; v1 P
  292.       background='#fffafafa',) `- ?" Y  J+ B! V. C& R5 A' A8 x
  293.       layout_height="fill",
    : @. V- U  ]: W  C; @4 ^; Y0 U' i
  294.       layout_weight="1.0",
    & E* d- r/ [" W( R7 [2 ]6 }
  295.     },$ J& V4 `, v$ K
  296.   },4 h3 i1 B8 X  o7 Q' a  q
  297. }0 K3 A' B! v- [# Y
  298. activity.setTheme(android.R.style.Theme_DeviceDefault_Light)--设置md主题# D0 h! T4 ]3 O5 e
  299. activity.setContentView(loadlayout(layout))
    , f/ Y' l3 @6 x
  300. ( G  z' S6 U/ O3 r. z' r5 m
  301. function click(v) local t=v.getText() if t=="¡û" then local i=input.getText().toString() input.setText(string.sub(i,1,-2)) else input.append(t) end) d/ s( I7 L. Z; f  E* y9 K
  302.   local s,r=pcall(dostring,"return "..input.getText().toString())
    ( |) i6 w5 X4 e5 R$ g: a
  303.   if s then
    + z( j" G3 ~& L7 N" X( D: t
  304.     if r then$ V, u' b3 t% B' P5 R
  305.       output.setText(tostring(r))9 z& S5 M2 k3 J2 ]* U4 H
  306.      else7 L* x% s- i' i
  307.       output.setText("")3 |& |" X/ o# x+ h: D
  308.     end3 D. c8 s& D) z
  309.   end5 d5 B/ m* o8 d7 O8 \
  310. end
    9 m* t( K  B5 \! ]- P! m' j: M) {

  311. ! ^, X, }1 i2 Z3 x% M
  312. function clear()" l# R, P" O2 h1 B6 c7 Y
  313.   input.setText("")
    : I5 P3 w8 t; k( g2 |3 P
  314.   output.setText("")# P, ?- a5 [2 D( O1 Z1 |
  315. end
    7 y2 G; P+ `; u* y. z
  316. function up()
    - b  h# \8 a. J: h
  317.   input.setText(output.getText())
    6 S1 l+ p. r7 e
  318. end
    ) n/ k5 c) }% ^$ S. _; g
  319. 9 M& ?' O- d) Y$ @# V+ J
  320. function dostring(s); u+ c; H+ G% e6 R( y9 p, L
  321.   return assert(loadstring(s))()
    $ X( V: t" v$ c' ?* t0 t/ z* b
  322. end
    ( d+ \2 E& q6 h7 v

  323. 8 M6 H' i3 C- J  Z
  324. 0 Q* A9 w$ r! ]8 {& D
  325. function enter()! _2 r/ M. X/ n' _% P- I7 Z
  326.   local s,r=pcall(loadstring("return "..input.getText().toString()))
    0 _1 s1 M7 R# q& U6 y
  327.   if s then
    . Y+ n3 W1 A9 S) H4 s3 W
  328.     if r then
    9 t( C9 d: X2 T( j8 p- X! _$ }# ~
  329.       output.setText(tostring(r))
    # e3 K5 Z0 D- ~6 E( R0 P# X
  330.      else" ^, W  |4 {- W' k$ U9 K
  331.       output.setText("")
    ; `3 V4 f; S* m, A  V, e. e" P
  332.     end7 a( U6 F$ C: R) V7 X
  333.    else
    ( E; ]  A/ f; \. s4 \) U
  334.     output.setText("输入错误")
    9 R* m1 {# |5 H3 Q2 H1 I6 T
  335.   end5 o" E$ i9 w( b, v1 f
  336. end
    7 V1 O  d  P7 U) F4 a+ t9 O
  337. function 菜单()
    . K5 }, t; j% p5 J( C6 L' J& ?
  338.   pop=PopupMenu(activity,endg)# H* c6 Z1 A3 Q/ g
  339.   menu=pop.Menu
    & s) D/ f0 X& V2 Z, C! {% r) D" [
  340.   menu.add("⌨     用输入法输入").onMenuItemClick=function(a)9 O6 u: ]. ]* R
  341.     --输入对话框(自定义布局对话框,带输入框的)# X, L% S' h$ q6 ~
  342.     InputLayout={5 m1 ?5 n5 ^: ?" ]8 ~
  343.       LinearLayout;
    5 z! h6 n9 @. Z5 y
  344.       orientation="vertical";--重力属性
    % ^8 x9 V( c3 m% y' c3 O/ a% N4 K# P
  345.       Focusable=true,--可聚焦
    4 i* ^$ h6 l- ^. j# d6 t- ]
  346.       FocusableInTouchMode=true,--可聚焦在触摸模式下,可变色
    ' g8 g+ t& l" J2 g9 j
  347.       {; X5 W, T' D* o9 l& J9 ?( M9 M
  348.         TextView;--文本控件
    ( a# `( k+ }, z
  349.         id="Text",4 U2 P" F6 l4 _3 n3 }* A$ r
  350.         layout_width='80%w';--文本宽度& P7 E1 e4 z+ }3 k/ L9 E3 [- m
  351.         layout_marginTop='10dp';--布局顶距
    9 m. D- [6 O- s! W
  352.         layout_marginLeft='3dp';--布局左距8 M. b2 F/ o$ [/ v
  353.         layout_gravity='center';--重力属性
    # k# X3 c( I/ x/ K* m6 p# w& K5 P
  354.         text='输入想要输入的内容:';--显示的文字
    , o- i( D- D+ P, f& r. c3 g0 f+ P" A. ^
  355.         textSize='15sp';--文字大小
    0 W& N. h  b7 [' W- b% L9 y# B. E
  356.       };; s* W) g+ I" D8 F
  357.       {2 T! n: B  _# E
  358.         EditText;--编辑框控件
    0 r. |' p5 R; C& L- m. Q, T
  359.         id="edit";
    * {/ S8 Z- P/ |' R
  360.         layout_width='80%w';--编辑框宽度
    # ]% e; x3 T( g. X3 N  m
  361.         layout_marginTop='5dp';--布局顶距2 U( M/ e2 t, |6 G1 u) o7 w. f0 X
  362.         layout_gravity="center",--重力属性- k' `& d9 m& {  g' r
  363.         Hint='请输入...';--设置编辑框为空时的提示文字) t7 l! q. F# I7 T6 ~7 ^
  364.       };
    # J0 F: e: v  b" t; z1 g; g
  365.     };
    & E4 O) z1 U3 a+ y  o
  366. , M% S3 b. F. r3 T0 p: q9 H- R. Q% b
  367.     AlertDialog.Builder(this)
      x# v2 m* E" `
  368.     .setTitle("用输入法输入")--设置标题
    6 ~/ }5 |9 P1 {# x  C& w
  369.     .setView(loadlayout(InputLayout))--设置布局% ], U- E/ k; G7 K0 K0 \
  370.     .setPositiveButton("确定",function(v)--设置积极按钮( O8 W; t$ M3 {8 U2 @6 I
  371.       input.setText(edit.Text)% D5 k5 i* w7 _% R! T. R
  372.     end)0 O/ m- E/ Z1 D* {
  373.     .setNegativeButton("取消",nil)--设置消极按钮
    ! D7 d* q# O/ [% M1 A" P
  374.     .show()--显示弹窗
    # v2 @! m) a& E3 e' z* q6 j+ V
  375.     import "android.view.View$OnFocusChangeListener"
    ! |+ t) T' C/ w& G0 v
  376.     edit.setOnFocusChangeListener(OnFocusChangeListener{; E0 O7 a) o# u9 E' o% {" P
  377.       onFocusChange=function(v,hasFocus)* K- J5 ~4 K7 f
  378.         if hasFocus then7 E7 w% }# R9 l/ H1 O
  379.           Text.setTextColor(0xFD009688)--当编辑框被点击时修改标题下的文本颜色
    ) h* ~9 i1 s  J) n7 L0 N+ ]9 M
  380.         end0 y4 P- g# a2 h& ]4 |" B
  381.       end})$ d6 s  b: b# p5 f2 V9 j
  382.   end7 v- H6 B9 Q' o2 v
  383.   pop.show()--显示@小米~! s4 ^, t( W- i. f! E! i9 W
  384. end
    3 w' u+ p$ ^( w3 e3 d1 V
  385. --activity.setTheme(android.R.style.Theme_Material_NoActionBar)0 c' n, t; H% ]4 ^& T; \/ Q
  386. 5 O% l8 ~  _  Y1 i( K# s. i# }

  387. ) d* W" ?! g$ ]
复制代码

0

主题

2

帖子

8

积分

新手上路

Rank: 1

积分
8
发表于 2022-7-7 16:43:16 | 显示全部楼层 来自:俄罗斯

uopvaupx

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|星空社区 |网站地图

GMT+8, 2024-4-25 08:14 , Processed in 0.067684 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表