安卓 |显示一个数的乘法表
给定一个数字,任务是使用Android App显示这个数字的乘法表。
构建应用程序的步骤:
- STEP-1:打开activity_main.xml文件并添加 TextView、EditText 和一个 Button
- 第 2 步:为每个组件分配 ID
- STEP-3:现在,打开 MainActivity 文件并声明变量。
- 第 4 步:使用上面 XML 代码中设置的 id 读取在 EditText 框中输入的值。
- 第 5 步:向“添加”按钮添加点击侦听器
- 第 6 步:单击 Add 按钮后,我们需要将值相乘并将其存储在 Buffer 中
- STEP-7:然后通过在 TextView 中设置缓冲区,在 TextView 中显示结果输出。
执行:
文件名:activity_main.xml
XML
Java
// Build the java logic for multiplication table
// using button, text view, edit text
package com.example.windows10.table;
import android.app.Dialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity
extends AppCompatActivity
implements View.OnClickListener {
// define the global variable
// variable number1, number2 for input input number
// Add_button, result textView
EditText editText;
Button button;
TextView result;
int ans = 0;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// by ID we can use each component
// whose id is assigned in the XML file
editText = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
result = (TextView)findViewById(R.id.textView);
// set clickListener on button
button.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
switch (v.getId()) {
case R.id.button:
StringBuffer buffer = new StringBuffer();
int res;
// get the input number from editText
String fs = editText.getText().toString();
// convert it to integer
int n = Integer.parseInt(fs);
// build the logic for table
for (int i = 1; i <= 10; i++) {
ans = (i * n);
buffer.append(n + " X " + i
+ " = " + ans + "\n\n");
}
// set the buffer textview
result.setText(buffer);
break;
}
}
}
文件名:MainActivity。Java
Java
// Build the java logic for multiplication table
// using button, text view, edit text
package com.example.windows10.table;
import android.app.Dialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity
extends AppCompatActivity
implements View.OnClickListener {
// define the global variable
// variable number1, number2 for input input number
// Add_button, result textView
EditText editText;
Button button;
TextView result;
int ans = 0;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// by ID we can use each component
// whose id is assigned in the XML file
editText = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
result = (TextView)findViewById(R.id.textView);
// set clickListener on button
button.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
switch (v.getId()) {
case R.id.button:
StringBuffer buffer = new StringBuffer();
int res;
// get the input number from editText
String fs = editText.getText().toString();
// convert it to integer
int n = Integer.parseInt(fs);
// build the logic for table
for (int i = 1; i <= 10; i++) {
ans = (i * n);
buffer.append(n + " X " + i
+ " = " + ans + "\n\n");
}
// set the buffer textview
result.setText(buffer);
break;
}
}
}
输出: