1

私はこれcustom dialogを中に持ってActivtyActivityGroupます。

外をクリックするとダイアログが閉じて、オンラインで見つけたすべてのことを試してみました。

私は試しましたsetCanceledOnTouchOutside(true)-動作しませんでした

私はもう試した:

public boolean onTouchEvent ( MotionEvent event ) {
  // I only care if the event is an UP action
  if ( event.getAction () == MotionEvent.ACTION_UP ) {
    // create a rect for storing the window rect
    Rect r = new Rect ( 0, 0, 0, 0 );
    // retrieve the windows rect

    this.getWindow ().getDecorView ().getHitRect ( r );
    Log.i(r.toShortString(),r.toShortString());
    // check if the event position is inside the window rect
    boolean intersects = r.contains ( (int) event.getX (), (int) event.getY () );
    // if the event is not inside then we can close the activity
    if ( !intersects ) {
      // close the activity
      this.dismiss ();
      // notify that we consumed this event
      return true;
    }
  }

そしてそれもうまくいきませんでした。

LogCatに表示されているように、何らかの理由でダイアログウィンドウのサイズが全画面表示になっているため、「外部」に触れることができないと思います。

私はそれが活動グループと何かをしなければならないかもしれないと思います..何か提案はありますか?

4

1 に答える 1

2

よく考えた後、私は最も簡単な解決策を見つけました:

問題:

何らかの理由で(私が使用したテーマはダイアログであり、全画面表示ではありませんが)、getWindow().getDecorView()画面全体をカバーするビューが返されます。

ソリューション:

XMLファイルで、ルート要素にIDを指定し、上記の関数を次のように変更しました。

private View rootView;

public BaseDialog(Context context, int theme) {
    super(context, theme);  
    //I don't think the next 2 lines are really important - but I've added them for safety  
    setCancelable(true); 
    setCanceledOnTouchOutside(true);    
}

public void setRootView(int resourceId)
{
    this.rootView = findViewById(resourceId);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    Rect rect = new Rect();
    rootView.getHitRect(rect);
    if (!rect.contains((int)event.getX(), (int)event.getY()))
    {
        this.dismiss();
        return true;
    }
    return false;       
}       

それが誰かを助けることを願っています...:)

于 2013-01-28T15:47:17.977 に答える