動的に二次元配列のボタンを配置し、クリックしたときにそのボタンの座標を取得の仕方が分からないです。取得して次のページにIntentで渡したいです。もとのコード削ってるのでつじつまが合わないところもあるかもしれませんが大体こんな感じです。
動的に配置されてるのでR.id.~が使えないや、二次元配列でonClickListenerの使い方がわからないです。どうぞよろしくお願い致します。
public class Main extends AppCompatActivity { private String messagea,messageb,f,g; private GridLayout layout; private Button[][] button; private int i,j,y,x,ax,ay,bx,by,cx,dx,ex,fx,gx; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); layout = new GridLayout(this); setContentView(R.layout.activity_main); Intent intent = getIntent(); messagea = "9"; messageb = "9"; ax = Integer.parseInt(messagea); ay = Integer.parseInt(messagea); bx =Integer.parseInt(messageb); by =Integer.parseInt(messageb); fx=Integer.parseInt(f); gx=Integer.parseInt(g); layout=(GridLayout) findViewById(R.id.botan); button = new Button[10][10]; for(i=0; i<ax;i++) { for(j=0;j<bx;j++){ button[i][j] = new Button(this); layout.setColumnCount(ax); layout.setRowCount(bx); button[i][j].setTag(String.valueOf(i)); button[i][j].setTag(String.valueOf(j)); GridLayout.LayoutParams[][] params = new GridLayout.LayoutParams[20][20]; params[i][j] = new GridLayout.LayoutParams(); params[i][j].width = 130; params[i][j].height = 130; params[i][j].columnSpec = GridLayout.spec(i); params[i][j].rowSpec = GridLayout.spec(j); button[i][j].setLayoutParams(params[i][j]); button[i][j].setText("a"); button[i][j].setLayoutParams(params[i][j]); layout.addView(button[i][j]); } } for(y=0; y<ay;y++) { for (x = 0; x < by; x++) { button[y][z].setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String arrayi = v.getTag(i).toString(); String arrayj = v.getTag(j).toString(); Intent intent = new Intent(Main.this, Next.class); intent.putExtra(xx, arrayi); intent.putExtra(yy, arrayj); startActivity(intent); } }); } } } } コード
参考にしたサイトはこちらです
https://akira-watson.com/android/activity-2.html
https://groups.google.com/forum/#!topic/android-group-niigata/HZP--LtazVw
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/22 18:39