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 {
            
        }
    }
}

你可能感兴趣的:(Android,系统开发,java)