0

コーディングの基礎を学ぶのに役立つ簡単なアプリを作成しています。ユーザーが をクリックするImageButtonと、次の 2 つのことが起こります。

  1. Button画像がランダム画像に変わります。
  2. が画面上のButtonランダムな位置に移動します。

これらの両方を個別に行う方法を理解しましたが、両方を一緒にすると、onClick一度に 1 つしか機能しません。

onClickコード:

ImageButton button = (ImageButton) findViewById(R.id.my_button);
    button.setOnClickListener(new OnClickListener() {
        public void onClick (View v) {

            // change button image
            int imgNo = (int) (Math.random() * 9) + 1; // 9 images in the folder, start at index 1
            int imgID = getResources().getIdentifier("chef" + imgNo, "drawable", getPackageName());
            v.setBackgroundResource(imgID);

            // move button to a random location
            LinearLayout button_container = (LinearLayout) findViewById(R.id.my_window);
            int x = (int) (Math.random() * (button_container.getWidth() - v.getWidth())); // might need to work out how to find if phone is landscape or portrait;
            int y = (int) (Math.random() * (button_container.getHeight() - v.getHeight()));
            v.layout(x, y, x + v.getWidth(), y + v.getHeight());

            Toast.makeText(WhackachefActivity.this, "X: " + x + " Y: " + y + " Width: " + v.getWidth() + " Height: " + v.getHeight() + " Image: " + imgNo, Toast.LENGTH_LONG).show();           
        }
    });

Toast、すべての変数が正しく機能していることを証明するためのものです。場所の変更または画像の変更コードのいずれかがコメント アウトされている場合、もう一方は正常に機能します。画像が現在の画像にランダムに設定されている場合 (つまり、画像に変化がない場合) は、ランダムな場所が機能しますが、それ以外の場合は、XML の既定の場所に設定されます。

参考までに、主なXMLものは次のとおりです。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/my_window" >

<ImageButton
    android:id="@+id/my_button"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="center"
    android:contentDescription="@string/description"
    />

フォローアップとして、2 つの関連する質問:

  1. アプリが最初にロードされたときに onClick コードを 1 回実行するにはどうすればよいですか?
  2. ImageButton 自体のサイズを自動的に作成するにはどうすればよいですか (つまり、ランダムな画像のサイズを引き伸ばすことなく、わずかに異なるサイズに合わせることができます)。
4

2 に答える 2

1

.layoutを使用する代わりに、LinearLayoutのパディングを使用してボタンを移動するという解決策を見つけました。メイン アクティビティ コード (途中でいくつかの小さな変更を加えたもの) は、次のようになります。

final ImageButton button = (ImageButton) findViewById(R.id.my_button);
final LinearLayout button_container = (LinearLayout) findViewById(R.id.my_window);

button.setOnClickListener(new OnClickListener() {
    public void onClick (View v) {

        // change button image
        int imgNo = (int) (Math.random() * 9) + 1; // 9 images in the folder, start at index 1
        int imgID = getResources().getIdentifier("chef" + imgNo, "drawable", getPackageName());
        button.setBackgroundResource(imgID);
        button.requestLayout(); // size to fit content

        // move button to a random location
        int x = (int) (Math.random() * (button_container.getWidth() - button.getWidth()));
        int y = (int) (Math.random() * (button_container.getHeight() - button.getHeight()));
        button_container.setPadding(x, y, 0, 0);
    }   
});
button.performClick(); // run once at the start to set image and position randomly
于 2012-06-15T12:30:53.463 に答える
1

さて、主な質問については、すぐに答えがありません.バックグラウンドリソースを削除するだけでうまくいく理由はわかりませんが、おそらくレイアウト後にバックグラウンドリソースを変更すると? 試してみる価値はありますが、それはなぜそれが起こるのかを本当に説明していません.

「ボーナスポイント」の質問には、次のように答えることができます。

1: 単純imageButton.performClick()にあなたを呼び出しますonCreate()(もちろん、あなたの を割り当てた後OnClickListener)。

2: を , に設定し、をImageButton呼び出します。次に、コンテンツに合わせてサイズを調整する必要があります。LayoutParamswrap_contentwrap_contentimageButton.requestLayout();

于 2012-06-13T14:26:07.080 に答える