加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

演示用Groovy开发Android程序,实现一个IOS风格计算器界面(无计

发布时间:2020-12-14 17:10:25 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 package com.aruis.android.androidhomework1import android.app.Activityimport android.graphics.Colorimport android.os.Bundleimport android.uti

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

package com.aruis.android.androidhomework1

import android.app.Activity
import android.graphics.Color
import android.os.Bundle
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.LinearLayout.LayoutParams
import android.widget.TextView

public class MainActivity extends Activity {

    TextView textView;

    List buttonContents = [
            [E.AC,E.PN,E.PER,E.DIVIDE],[E.SEVEN,E.EIGHT,E.NINE,E.MULTIPLY],[E.FOUR,E.FIVE,E.SIX,E.MINUS],[E.ONE,E.TWO,E.THREE,E.PLUS],[E.ZERO,E.POINT,E.EQUAL]
    ]

    List buttonList = [];

    Long zeros = 1000000000000000
    Long zeros00 = zeros * 100

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main)

        DisplayMetrics dm = new DisplayMetrics()
        getWindowManager().getDefaultDisplay().getMetrics(dm)

        LayoutParams linearLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,1)
        LayoutParams layoutParams_zero = new LayoutParams(0,LayoutParams.MATCH_PARENT,2)
        LayoutParams layoutParams = new LayoutParams(0,1)

        textView = findViewById(R.id.textView)
        text = '0'
        int i = 0

        buttonContents.each {
            LinearLayout _layout = new LinearLayout(this);
            _layout.orientation = LinearLayout.HORIZONTAL

            _layout.layoutParams = linearLayoutParams
            int j = 0
            it.each { e ->
                MyButton btn = new MyButton(this,e);

                btn.layoutParams = e == E.ZERO ? layoutParams_zero : layoutParams
                btn.textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,20,dm)

                if (j == 3 || (i == 4 && j == 2)) {
                    btn.backgroundResource = R.drawable.button_style_right
                    btn.textColor = Color.WHITE
                } else if (i == 0) {
                    btn.backgroundResource = R.drawable.button_style_top
                } else {
                    btn.backgroundResource = R.drawable.button_style
                }

                btn.setOnClickListener(clickListener)

                buttonList.add(btn)
                _layout.addView(btn)
                j++
            }
            ViewGroup contentParent = (ViewGroup) findViewById(16908290); //可以取得 activity_main.xml指定的布局
            LinearLayout _view = contentParent.getChildAt(0)
            _view.addView(_layout)
            i++
        }
    }

    /**
     * 计算功能未实现
     */
    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        void onClick(View view) {
            E data = view.data;
            switch (data) {
                case E.AC:
                    text = '0'
                    break
                case E.PN:
                    if (text.indexOf('-') == 0)
                        text = text.substring(1)
                    else
                        text = '-' + text
                    break
                case E.PER:
                    text = text != '0' ? (text.toFloat() * zeros) / zeros00 : '0'//逻辑尚有bug
                    break
                case E.POINT:
                    if (text.indexOf('.') > -1)
                        break;
                case (E.POINT..E.NINE):
                    if (text == '0' && data.toString() != '.')
                        text = data.toString()
                    else
                        text += data.toString()
                    break
            }

        }
    }

    def setText(String s) {
        textView.text = s
    }

    String getText() {
        return textView.text
    }

}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读