Toast 定时退出,一直显示的方法,android无焦点提示框

来源:互联网 发布:域名再长狼也能记住 编辑:程序博客网 时间:2024/06/01 07:57

有时我们需要一个提示框,但这个提示框还不能获取焦点(比如视频播放,不能让提示框获取后面控制面板的焦点),但是,如一直快进android的提示框Toast又不能定时退出,或者显示时间不理想;网上看了许多例子,有用定时器handler来控制定时显示的,继承Toast方法写的,都不是太理想;这里我们用反射加部分重写,来实现它:

1,CustomToast.java

public class CustomToast extends Toast {private boolean isShowing = false;public CustomToast(Context context) {// TODO Auto-generated constructor stubsuper(context);}public CustomToast(Context c, View v) {this(c);setView(v);}/****************** 自己写的接口方法 **************************/// 让view 一直显示;取消用cancel方法,其他用法同Toast;public void showAlways() {Field mTNField = null;try {mTNField = Toast.class.getDeclaredField("mTN");// 获取权限mTNField.setAccessible(true);// 得到实例,这里反射不用newinstance;Object mTN = mTNField.get(this);try {// android 4.0以上系统要设置mT的mNextView属性Field mNextViewField = mTN.getClass().getDeclaredField("mNextView");mNextViewField.setAccessible(true);mNextViewField.set(mTN, getView());} catch (NoSuchFieldException e) {e.printStackTrace();}Method showMethod = mTN.getClass().getDeclaredMethod("show", null);showMethod.setAccessible(true);showMethod.invoke(mTN, null);isShowing = true;} catch (Exception e) {e.printStackTrace();}}@Overridepublic void show() {// TODO Auto-generated method stubsuper.show();isShowing = true;}// 显示多少豪秒自动退出;用了show方法的多态;public void show(long delayMillis) {// TODO Auto-generated method stub// 定时退出showAlways();new Handler().postDelayed(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubcancel();}}, delayMillis);}@Overridepublic void cancel() {// TODO Auto-generated method stubsuper.cancel();isShowing = false;}// 判断toast是否正在显示public boolean isShowing() {return isShowing;}/****************结束接口******************/}

2,测试文件MainActivity.java

public class MainActivity extends Activity {private Button showtoast, closetoast;private String TAG = "MainActivity";ReflectToast1 rToast = null;private boolean isDestoryed = false;CustomToast customToast = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);showtoast = (Button) findViewById(R.id.show);closetoast = (Button) findViewById(R.id.hide);showtoast.setOnClickListener(new MyOnClickListener());closetoast.setOnClickListener(new MyOnClickListener());TextView tvTextView = new TextView(this);tvTextView.setText("wocalei");tvTextView.setTextColor(this.getResources().getColor(android.R.color.holo_blue_dark));tvTextView.setBackgroundColor(0xffffffff);customToast = new CustomToast(this);customToast.setView(tvTextView);// 这个线程检测isShowing()函数new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubwhile (!isDestoryed) {Log.i(TAG, "is show = " + customToast.isShowing());try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}).start();}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();isDestoryed = true;}class MyOnClickListener implements View.OnClickListener {@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.show:// customToast.show();// customToast.show(5000);customToast.showAlways();break;case R.id.hide:customToast.cancel();break;default:break;}}}}

完美解决问题!

0 0
原创粉丝点击