在TextView中显示乘法表

时间:2016-07-29 11:34:47

标签: android

我正在尝试生成乘法表,其中第一个EditText采用实际数字,而第二个EditText采用乘法表的实际范围。当我运行项目时,结果只有数字 * range .. 任何人都可以帮我处理下面提到的循环或代码或者,在GridLayoutTableLayout而不是TextView中显示表格的任何替代方法。

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_multiplication_table);
    number = (EditText)findViewById(R.id.numberTable);
    range = (EditText)findViewById(R.id.numberRange);
    click = (Button)findViewById(R.id.click);
    result = (TextView)findViewById(R.id.display);
    final String x = range.getText().toString();
    click.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
          int   a = Integer.parseInt(number.getText().toString());
          int   b = Integer.parseInt(range.getText().toString());



             for(int i = 1 ; i <= 10; i++)
            {
                for(int j = 1 ; j <= 10; j++)
                {
                    int res = a * b;
                            result. setText(a +" * " + b + " = " + res);

                }





            }
            return;
        }
    });

}
}

1 个答案:

答案 0 :(得分:1)

您要为每一行调用UTF8_STRING TEXT STRING text/plain;charset=UTF-16 text/plain;charset=UTF-8 text/plain;charset=UTF-16BE text/plain;charset=UTF-16LE text/plain;charset=ISO-8859-1 text/plain;charset=US-ASCII text/plain FILE_NAME text/uri-list text/uri-list;charset=UTF-16 text/uri-list;charset=UTF-8 text/uri-list;charset=UTF-16BE text/uri-list;charset=UTF-16LE text/uri-list;charset=ISO-8859-1 text/uri-list;charset=US-ASCII x-special/gnome-copied-files ,但setText()会重置setText()的文字,您可能希望使用TextView而不是

append()

或者可以使用result.setText(""); int a = Integer.parseInt(number.getText().toString()); int b = Integer.parseInt(range.getText().toString()); for(int i = 1 ; i <= b; i++){ int res = a * i; result.append(a +" * " + i + " = " + res + "\n"); }

StringBuilder