使用TableLayout实现9*9乘法表  动态创建布局的方式 输入框最小输入的值是1,最大是9

 

布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
        <EditText
            android:maxLength="1"
            android:inputType="number"
            android:digits="123456789"
            android:id="@+id/edit_count"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:hint="请输入需要计算的数值范围(1-9)"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:textColor="@color/colorPrimary"/>
        <Button
            android:id="@+id/start_btn"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开始"/>
        <TableLayout
            app:layout_constraintTop_toBottomOf="@+id/edit_count"
            app:layout_constraintLeft_toLeftOf="parent"
            android:orientation="horizontal"
            android:id="@+id/table_layout"
            android:gravity="left"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </TableLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity文件

package com.suoer.comeonhealth.demomultiplicationtableapplication;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private TableLayout table_layout;
    private EditText edit_count;
    private int totalCount = 1;
    private Button start_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        table_layout = findViewById(R.id.table_layout);
        edit_count = findViewById(R.id.edit_count);
        start_btn = findViewById(R.id.start_btn);
        edit_count.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                table_layout.removeAllViews();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        initTableView();
    }

    private void initTableView() {
        start_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String count = edit_count.getText().toString().trim();
                if (TextUtils.isEmpty(count)) {
                    Toast.makeText(MainActivity.this, "请输入行count", Toast.LENGTH_SHORT).show();
                    return;
                }
                totalCount = Integer.valueOf(count);
                for (int i = 1; i <= totalCount; i++) {
                    TableRow tablerow = new TableRow(MainActivity.this);
                    TableLayout.LayoutParams params = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
                    tablerow.setLayoutParams(params);
                    for (int j = 1; j  <=totalCount; j++) {
                        TextView textView = new TextView(MainActivity.this);
                        textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
                        if(j<=i){
                            textView.setText(j + "*" + i + "=" + j * i);
                        }else{
                            textView.setText("");
                        }
                        tablerow.addView(textView);

                    }
                    table_layout.addView(tablerow);
                }
            }
        });


    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值