2017-07-05 4 views
0

まだAndroidスタジオが新しく、希望の行と列を持つGridLayoutを作成してEditTextで埋めてテストしたいと思っています。しかし、私はEditTextコンストラクタのパラメータとして何を置くべきかわからない問題に遭遇します。ここで は、以下のJavaコードです:Android StudioのEditTextコンストラクタのパラメータには何を入れる必要がありますか?

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //The numbers WILL vary but this is just a test 
    int rows = 3; 
    int columns = 4; 

    GridLayout gd = (GridLayout) findViewById(R.id.grid1); 
    gd.setRowCount(rows); 
    gd.setColumnCount(columns); 
    EditText edt; 

    for(int r = 0; r < rows; r++) 
    { 
     for(int c = 0; c < columns; c++) 
     { 
      //What to put in the parameters in this code below? 
      edt = new EditText(); 
      gd.addView(edt); 
     } 
    } 
} 

そして、ここではXMLです:

<GridLayout 
     android:id="@+id/grid1" 
     android:layout_width="368dp" 
     android:layout_height="495dp" 
     tools:layout_editor_absoluteX="8dp" 
     tools:layout_editor_absoluteY="8dp"> 

</GridLayout> 

私はのEditTextのコンストラクタを見て、そのコンテキストが必要とされて見つけることが、どのように私は、中にいることを置けばいいのそこ?これが正しい方法でない場合は、正しい方法を教えてください。

答えて

1

すべてのアクティビティはコンテキストです。ビューの場合は、便利なことに応じて、現在のアクティビティ、または親のコンテキスト(getContext()経由)を渡します。ここでは一般的にはアクティビティなので、これを渡します。

+0

ありがとうございました!私はそれを忘れていた –

0
のEditTextのための最初の使用findviewbyIdで

、次いで のEditText編集=(のEditText)findViewById(R.id.edit_id)例えばその編集テキストオブジェクト

に設定されたテキストを使用します。 edit.setText( "abc");

関連する問題