布局文件如下:
<?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);
}
}
});
}
}