0
在android studio的TextView字段中打印/顯示二維數組(int)的值的代碼是什麼? 當用戶在EditText字段中放置行,列值並單擊該按鈕時,我希望TextView結果出來。什麼是在textView字段上打印2D int數組值的方法 - android studio?
public class MainActivity extends AppCompatActivity {
EditText value01, value02;
Button show;
TextView result;
int mpaTable[][] = new int[4][4];
int[][] MpaTable = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value01 = (EditText)findViewById(R.id.value01);
value02 = (EditText)findViewById(R.id.value02);
show = (Button)findViewById(R.id.button);
result = (TextView)findViewById(R.id.result); }
public void btnClick(View V){
int a = Integer.parseInt(value01.getText().toString());
int b = Integer.parseInt(value02.getText().toString());
result.setText(mpaTable[a][b]);
}
}
我試着用上面的代碼,但結果出現錯誤。 –
你得到了什麼樣的錯誤?確保您將文本字段設置爲字符串。現在MpaTable返回一個int,所以你需要添加String.valueOf(mpaTable [a] [b]); – Brion
我改成了result.setText(String.valueOf(mpaTable [a] [b]));但放置數據後結果顯示爲0。 –