package com.ljmin;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button button;
public PopupWindow mPopupWindow;
private boolean flag = false; // false表示没显示
public Thread thread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
//触摸监听
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: //按下监听
Log.i("log", "flag:---"+flag);
//改变显示状态并改变标记
if (!flag) {
flag = true;
check();
//设置mPopupWindow为显示,并设置位置
mPopupWindow.showAtLocation(findViewById(R.id.linear),
Gravity.RIGHT | Gravity.BOTTOM, 0, 0);
//如果当前为显示,则启动线程
if (flag = true) {
thread = new Thread(new MyRunnable());
thread.start();
}
} else {
flag = false;
mPopupWindow.dismiss();
}
Log.i("log", "flag:+++"+flag);
break;
case MotionEvent.ACTION_UP: //抬起监听
break;
default:
break;
}
//注意:这里要返回true
return true;
}
class MyRunnable implements Runnable{
int i = 0;
@Override
public void run() {
while (i <= 5 && flag) {
i++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i("log", "i:" + i);
//这里设置为5秒后消失,并改变标记
if (i == 5) {
mPopupWindow.dismiss();
flag = false;
}
}
}
}
private void check() {
//绑定布局
View view = getLayoutInflater().inflate(R.layout.control, null);
//mPopupWindow不显示时
if (mPopupWindow == null) {
//设置mPopupWindow
mPopupWindow = new PopupWindow(view, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
//注意button要从绑定的布局里拿
button = (Button) view.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "---------", 1).show();
}
});
}
//mPopupWindow显示时设为消失
if (mPopupWindow.isShowing()) {
mPopupWindow.dismiss();
}
}
}