I have to iterate through a 2D loop step by step after pressing a button in android, lets say
我想在按下android中的一个按钮后,我必须逐步遍历2D循环
3 4 3 4 5 4 2 3 4
3 4 3 4 5 4 2 3 4
Output should be:
输出应该是:
After Button pressed:3 After Button pressed:4 After Button pressed:3 ....... ....... After Button pressed:4
按下按钮后:3按下按钮后:4按下按钮后:3 ....... .......按下按钮后:4
How should I use clicklistener in the loop?
我应该如何在循环中使用clicklistener?
1 个解决方案
#1
0
Try Like this:
试试这样:
public class Main extends AppCompatActivity {
int[][] array = new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int rows = 0;
int columns = 0;
TextView textView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.text);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (columns < array[rows].length) {
textView.setText("After Button pressed: " + array[rows][columns]);
columns++;
} else {
rows++;
if (rows != array.length) {
columns = 0;
textView.setText("After Button pressed: " + array[rows][columns]);
columns++;
} else {
rows = 0;
columns = 0;
}
}
}
});
}
#1
0
Try Like this:
试试这样:
public class Main extends AppCompatActivity {
int[][] array = new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int rows = 0;
int columns = 0;
TextView textView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.text);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (columns < array[rows].length) {
textView.setText("After Button pressed: " + array[rows][columns]);
columns++;
} else {
rows++;
if (rows != array.length) {
columns = 0;
textView.setText("After Button pressed: " + array[rows][columns]);
columns++;
} else {
rows = 0;
columns = 0;
}
}
}
});
}