触摸android后关闭视图

触摸android后关闭视图,android,ontouchlistener,Android,Ontouchlistener,我有一个活动,按下信息按钮后会显示出来。现在,我想在用户触摸屏幕时关闭/销毁活动,这样用户就可以再次回到主活动,而无需按下设备的“后退”按钮。代码是什么样子的 我试过这个,但没有效果: if(onTouchEvent(null)){ finish(); } 谢谢 编辑: 这是我的全部代码: public class InfoDialog extends Activity{ @Override protected void onCreate(Bundle savedInstan

我有一个活动,按下信息按钮后会显示出来。现在,我想在用户触摸屏幕时关闭/销毁活动,这样用户就可以再次回到主活动,而无需按下设备的“后退”按钮。代码是什么样子的

我试过这个,但没有效果:

if(onTouchEvent(null)){
        finish();
}
谢谢


编辑:

这是我的全部代码:

public class InfoDialog extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.info_dialog); }
我使用这个类只是为了在android风格的Theme.Dialog中显示有关应用程序的简短信息
但是,我想添加一个功能,在按下触摸屏时销毁/关闭此活动,这样就不需要按下后退按钮。

您应该覆盖onTouch(视图v,运动事件) 诸如此类:

onTouch(View v, MotionEvent event){
   switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
         finish();
   }
}

为显示您的信息的视图提供id。然后在该视图上设置onTouch侦听器,如果使用activity,则完成该活动;如果使用visibility,则使其不可见

main = (RelativeLayout) findViewById(R.id.main);
main.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                        case MotionEvent.ACTION_DOWN:
                finish();
            }
        }

info = (RelativeLayout) findViewById(R.id.info);

info = (RelativeLayout) findViewById(R.id.info);
info.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                         case MotionEvent.ACTION_DOWN:
                info.setVisibilty(View.GONE);
            }
        }
试试这个

LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayoutId);

mainLayout.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                int eid = event.getAction();
                switch (eid) {
                case MotionEvent.ACTION_DOWN:
                    finish();
                    break;
                }
                return true;
            }
        });

您使用了什么,您是否使该视图可见或使用活动来显示信息?没有足够的代码来回答您的问题。您的意思是如果(onTouchEvent!=null)??我使用活动。我想在按下触摸屏后破坏活动。