10

ボタンを見つけるのに問題があります。AlertDialog5つのオプションのいずれかを選択する場所があります。オプションを選択すると、クリックしたボタンの色を変更できます。内部のxmlファイルでボタンを宣言しましたが、メソッド<RealativeLayout>を使用してID(IDは「id1」、「id2」のようなものです...)でボタンを見つけようとすると、エラーが発生し、できないというエラーが表示されますfindViewById私のようにこの方法を使用します:

AlertDialog.Builder builder = new AlertDialog.Builder(StartGameActivity.this);

builder.setTitle(R.string.pickColor);
builder.setItems(R.array.colorArray, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        Button btn_tmp;
        String theButtonId = "id";
        theButtonId = theButtonId+(String.valueOf(which));
        btn_tmp = (Button) findViewById(theButtonId);
    }
});

どうすればそれを修正できますか、それとも他の方法を使用する必要がありますか?

編集:

私は私の問題を解決したと思います。ボタンのメソッドの 1 つを使用しました: getId() 次のように:

final int id = clickedButton.getId();
final ImageButton btn_tmp;
btn_tmp = (ImageButton)findViewById(id);
4

4 に答える 4

4

背景: ID は本質的に変数です

ファイル内でXMLリソースに ID を指定すると、コンパイラはこれらすべてを使用してgen/R.java. 基本的に、これらの ID はクラス R に属する int 変数です。R.java の例:

// btw this file should never be edited!
public final class R {
  public static final class id {
    public static final int id1=0x7f0100aa;
    public static final int id2=0x7f0100ab;
  }
}

String変数の有効な名前 ( ) を含む が存在するという理由だけで、R.id.id1その変数に魔法のようにアクセスすることはできません。これを行うには、 を使用できますreflection。ただし、この場合、これは不必要な複雑さであり、さらに遅くなると思います。

findViewByIdID が必要です (整数変数):

Stringこの関数にa を指定することはできません。整数値、特に の int 変数に対応する値を使用する必要がありますR.java。例えば:

findViewById(R.id.id1) // for your 1st button

解決:

整数値を動的に選択できます。

AlertDialog.Builder builder = new AlertDialog.Builder(StartGameActivity.this);

builder.setTitle(R.string.pickColor);
builder.setItems(R.array.colorArray, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        Button btn_tmp;
        int chosenID=-1;

        switch (which) {
        case 1:   chosenID=R.id.id1;
                  break;
        case 2:   chosenID=R.id.id2;
                  break;
        }
        btn_tmp = (Button) findViewById(chosenID);
    }
});

buttonDoThis、 など、より説明的な ID を使用することもお勧めしますbuttonDoThat

于 2013-01-11T21:32:31.773 に答える
3
btn_tmp = (Button)findViewById(R.id.YourButtonId);

整数ではなく文字列を渡しています。

于 2013-01-11T21:32:54.830 に答える
1

ダイアログに独自の layout.xml を使用している場合は、まずそれをインフレートして、ビューをダイアログ ビルダーに渡す必要があります。

LayoutInflater layoutInflater = (LayoutInflater) StartGameActivity.this.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);

final RelativeLayout customDialogView= (RelativeLayout) layoutInflater.inflate(R.layout.custom_dialog_view, null);


AlertDialog.Builder builder = new AlertDialog.Builder(StartGameActivity.this);

builder.setTitle(R.string.pickColor);
//give the custom view to your dialog builder.
builder.setView(customDialogView);
builder.setItems(R.array.colorArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
    Button btn_tmp;

    switch(which){

    //....cases

    case 1:
         btn_tmp = (Button) customDialogView.findViewById(R.id.button1);
         //set the color of the button selected
         break;

    case 2:
         btn_tmp = (Button) customDialogView.findViewById(R.id.button2);
          //set the color of the button selected
         break;
    }

     //....cases
}
});
于 2013-01-11T21:38:31.520 に答える
0

ID の変数名を保持する文字列で findViewById() を使用しようとしています。変数名は実行時ではなくコンパイル時にのみ認識されるため、Java はこの種の動的変数名をサポートしていません。あなたは何をしようとしているのですか?この問題を解決するには、別の方法を見つける必要があります。さらに説明してください。提案を行うことができます。

于 2013-01-11T21:37:02.097 に答える