• Handler 修改UI


    在一个Activit中显示一个100个按钮、每个按钮上显示一个随机数。再设计一个产生随机数的按钮和一个排序按钮,随机数按钮为100个按钮产生随机数,排序按钮对100个按钮上的数字排序。

    
    
        
        
            
    public class ListViewActivity extends AppCompatActivity implements View.OnClickListener {
        private int[] numbers = new int[100];
        GridLayout gridLayout;
        int[] randent = randent(numbers);
    
        private Handler mHandler = new Handler(Looper.getMainLooper());
    
        public int[] Sort(int[] arrays) {
            for (int i = 0; i < arrays.length - 1; i++) {
                for (int j = 0; j < arrays.length - i - 1; j++) {
                    int tmp = arrays[j + 1];
                    if (arrays[j] > arrays[j + 1]) {
                        arrays[j + 1] = arrays[j];
                        arrays[j] = tmp;
                    }
                }
            }
    //        System.out.println("冒泡排序:" + Arrays.toString(arrays));
            return arrays;
        }
    
        public int[] randent(int[] arrays) {
            for (int i = 0; i < arrays.length; i++) {
                arrays[i] = new Random().nextInt(100);
            }
            return arrays;
        }
    
        public void change(final int[] arrays) {
            gridLayout.removeAllViews();  // 清空GridLayout
    
            for (int i = 0; i < 100; i++) {
                final Button button = new Button(this);
                final int index = i;  // 保存当前i的值
    
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        // 设置按钮的布局参数
                        GridLayout.LayoutParams params = new GridLayout.LayoutParams();
                        params.width = 0;
                        params.height = 0;
                        params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f);
                        params.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f);
                        button.setText(String.valueOf(arrays[index]));
                        button.setLayoutParams(params);
    
                        // 设置按钮文本居中
                        button.setGravity(Gravity.CENTER);
                        gridLayout.addView(button);
                    }
                });
            }
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list_view);
    
            gridLayout = findViewById(R.id.gridLayout);
            findViewById(R.id.randnt).setOnClickListener(this);
            findViewById(R.id.Sort).setOnClickListener(this);
    
            change(randent);
        }
    
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.randnt) {
                randent = randent(numbers);
                // 在主线程中更新UI
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        change(randent);
                    }
                });
            } else if (v.getId() == R.id.Sort) {
                Sort(randent);
                // 在主线程中更新UI
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        change(randent);
                    }
                });
                Toast.makeText(this, "排序成功", Toast.LENGTH_LONG).show();
            }
            else {
                
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
  • 相关阅读:
    作业 day4
    2023秋招上岸必备软件测试面试题
    利用 Rainbond 云原生平台简化 Kubernetes 业务问题排查
    阿基米德优化算法(Matlab代码实现)
    UE5中APlayerController属性与方法列表(翻译中......)
    2023年中国辣椒红素产量、需求量及行业市场规模分析[图]
    nuScenes数据集浅探(待完善……)
    每日一库:lumberjack -- 日志轮换和管理
    【Linux】虚拟机项目部署与发布
    移动端echarts图表的自适应使用
  • 原文地址:https://blog.csdn.net/for_syq/article/details/134187504