我正在尝试生成用户输入的任意数字的乘法表。我已经为应用程序开发了接口,但无法理解从逻辑部分(编码)开始的位置。我想要的是当用户在EditText中输入一个数字然后在TextView(id:printArea)中应该以图像2中给出的格式显示输入数字的表格。[只是为了向您展示我在文本中使用的TextView printArea部分,我不知道该使用什么]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:weightSum="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="Please Enter a Number: "
android:textSize="20sp" />
<EditText
android:id="@+id/num"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginLeft="30dp"
android:textSize="20sp" />
</LinearLayout>
<Button
android:id="@+id/calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/log"
android:onClick="submitNumber"
android:text="Get Table" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:id="@+id/printArea" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/clear"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="1dp"
android:text="Clear" />
<Button
android:id="@+id/credits"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:text="Credits" />
<Button
android:id="@+id/exit"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginRight="2dp"
android:text="Exit" />
</LinearLayout>
</LinearLayout>
,MainActivity.java是:
package com.example.tara.multiplicationtable;
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 {
EditText num;
Button credits;
Button calculate;
Button clear;
Button exit;
TextView printArea;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num = new EditText(this);
clear = new Button(this);
calculate = new Button(this);
credits = new Button(this);
calculate = new Button(this);
printArea = new TextView(this);
num = (EditText) findViewById(R.id.num);
credits = (Button) findViewById(R.id.credits);
clear = (Button) findViewById(R.id.clear);
exit = (Button) findViewById(R.id.exit);
printArea = (TextView) findViewById(R.id.printArea);
calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//change the integer value into string
int num1 = Integer.parseInt(num.getText().toString());
// Perform action on click
for (int i = 1; i <= 10; i++) {
printArea.setText(num1 + "X" + i + "=" + i * num1);
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num.setText("");
}
});
credits.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, Credits.class);
startActivity(i);
}
});
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
}
}
启动应用程序时,它应显示为The initial state of application
如果用户输入为2 The Final Result
,我想让应用程序显示如下表格答案 0 :(得分:0)
创建Listview,然后在其适配器中添加表数据。 像这样制作适配器的布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="=" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
答案 1 :(得分:0)
以下是我用来构建我想要构建的乘法表的代码。 activity_main.xml文件的代码为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginStart="2dp"
android:text="@string/Enter_number"
android:textSize="16sp" />
<EditText
android:id="@+id/num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="2dp"
android:layout_marginStart="1dp"
android:digits="0123456789"
android:hint="@string/hidden_text"
android:inputType="number"
android:maxLength="3"
android:textSize="16sp" />
</LinearLayout>
<Button
android:id="@+id/calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/calculate"
android:imeOptions="actionDone"
android:textSize="16sp"/>
<!--android:onClick="submitNumber"-->
<TextView
android:id="@+id/printArea"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="20sp"
android:background="@drawable/backimage" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginStart="2dp"
android:textSize="16sp"
android:text="@string/clear"
/>
<Button
android:id="@+id/credits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="1dp"
android:layout_marginStart="1dp"
android:textSize="16sp"
android:text="@string/credits" />
<Button
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:layout_marginStart="1dp"
android:layout_weight="1"
android:textSize="16sp"
android:text="@string/exit" />
</LinearLayout>
</LinearLayout>
MainActivity.java文件的代码将是:
package com.example.tara.multiplicationtable;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText num;
Button credits;
Button calculate;
Button clear;
Button exit;
TextView printArea;
//private static final String result = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num = new EditText(this);
clear = new Button(this);
calculate = new Button(this);
credits = new Button(this);
calculate = new Button(this);
printArea = new TextView(this);
num = (EditText) findViewById(R.id.num);
credits = (Button) findViewById(R.id.credits);
clear = (Button) findViewById(R.id.clear);
exit = (Button) findViewById(R.id.exit);
calculate = (Button) findViewById(R.id.calculate);
printArea = (TextView) findViewById(R.id.printArea);
calculate.setOnClickListener(new View.OnClickListener() {
// Perform action on Get Table Button click
public void onClick(View v) {
if (num.getText().length() == 0 ){
printArea.setText(R.string.err_msg);
}
else {
//change the integer value into string
printArea.setText("");
int num1 = Integer.parseInt(num.getText().toString());
String result;
for (int i = 1; i <= 10; i++) {
result = (num1 + " X " + i + " = " + i * num1);
printArea.append("\n" + result);
}
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num.setText("");
printArea.setText("");
}
});
credits.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Credits.class));
}
});
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Good Bye, User!", Toast.LENGTH_SHORT).show();
finish();
}
});
}
}
感谢大家的指导。