0

理想的には、findViewById メソッドを使用して (xml ファイル内の) ビュー参照を取得できます。ただし、すべてのビューをインスタンス化したクラスがあります

    public View Initialise(Context context){

    private Button mBoardButtons[][] = new Button[gridSize][gridSize];
       private TableRow rowArr[] = new TableRow[gridSize];
        private TextView mInfoTextView;
        private TextView mPlayer;
        private TextView mPlayerCount;
     TableLayout tableLayout1 = new TableLayout(context);
        TableLayout.LayoutParams tableParams1 = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        tableParams1.topMargin = 0;
        tableLayout1.setLayoutParams(tableParams1);

        for ( int i =0 ; i < gridSize ; i++){
            rowArr[i] = new TableRow(context);
            TableRow.LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            rowParams.gravity=Gravity.CENTER_HORIZONTAL;
            rowArr[i].setLayoutParams(rowParams);
            for(int j = 0; j < gridSize ; j++){
                mBoardButtons[i][j] = new Button(context);
                mBoardButtons[i][j].setText(Integer.toString(i)+","+Integer.toString(j));
                mBoardButtons[i][j].setTextSize(150/gridSize);
                mBoardButtons[i][j].setMaxHeight(450/gridSize);
                mBoardButtons[i][j].setMaxWidth(600/gridSize);
                rowArr[i].addView(mBoardButtons[i][j]);

            }
            tableLayout1.addView(rowArr[i]);
        }


        mInfoTextView = new TextView(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.topMargin = 5;
            params.gravity=Gravity.CENTER;
        mInfoTextView.setLayoutParams(params);
        mInfoTextView.setText("Info");
        mInfoTextView.setTextSize(25);
        tableLayout1.addView(mInfoTextView);


        TableLayout tableLayout2 = new TableLayout(context);
        TableLayout.LayoutParams tableParams2 = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        tableLayout2.setLayoutParams(tableParams2);

        TableRow tableRow = new TableRow(context);

        mPlayer = new TextView(context);

        mPlayer.setText("Player: ");
        tableRow.addView(mPlayer);

        mPlayerCount = new TextView(context);

        mPlayerCount.setText(" ");

        tableRow.addView(mPlayerCount);


    return tableLayout1;

}

上記のコードは特定のクラスで定義されています (xml ファイルの代わりとしましょう)

Activity クラスのこれらすべての View 要素の参照を取得するにはどうすればよいですか。

プログラムで定義されたビュー参照を取得するための findViewById の方法に似た方法はありますか?

4

1 に答える 1

0

new でそれらを作成すると、参照が得られます。どこかに保存してください。

または、後で findViewByID を使用できるように setID() を使用することもできます。しかし、今すぐ保存できるときにそれを行うのはばかげています。

于 2013-05-09T18:26:56.140 に答える