2

Dialogクラスを使用することは可能ですが、そのようにはしたくありません。代わりに、起動時にポップアップされ、ポップアップにメッセージを表示するPopupWindowクラスを使用して実行する必要があります。私は無力です、それの後ろに何日も費やした後、これを得ることができません。ここで入手できれば幸いです。どうぞよろしくお願いします。また、私が望むものが得られなかった場合は、以下のスニペットを参照してください。

public class PopupActivity extends Activity implements OnClickListener {

    LinearLayout ll = new LinearLayout(this);
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(ll);
    }

    protected void onStart() {
        super.onStart();
        final PopupWindow pw;
        Button button = new Button(this);
        button.setText("Hello");
        pw = new PopupWindow(button, 245, 284, true);

        button.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View view) {  
                pw.dismiss();            
            }  
        });

        pw.showAsDropDown(ll, 10, -50);
    }
}

上記のコードは私にFORCECLOSEを与えます:/みんなを助けてください..

4

1 に答える 1

3

これは、ポップアップ ウィンドウを表示する方法です。

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:id="@+id/layoutTemp">

</LinearLayout>

popup_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="Test Pop-Up" />

</LinearLayout>

main.java

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

    new Handler().postDelayed(new Runnable() {
        public void run() {
            showPopup();
        }
    }, 100);
}


public void showPopup(){
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_layout, null, false), 100, 100, true);
    pw.showAtLocation(findViewById(R.id.layoutTemp), Gravity.CENTER, 0, 0);
}

アイデアは、アクティビティが読み込まれるとポップアップが表示されるということです。それ以外の場合は、例外Unable to add window -- token null is not valid;が生成されます。あなたの活動は進んでいますか?. ボタンのクリックでポップアップを表示しない限り、それが 100 ミリ秒の遅延 (ほとんど目立たない) の後にポップアップを表示する理由です。

于 2012-03-03T22:49:43.740 に答える