AndroidStudio下的点击事件不响应

本来是测试自定义Toast,发现

 implements View.OnClickListener的Onclick事件怎么都不响应,开始以为是自定义的问题。结果当然不是;需要clean项目就好了,AndroidStudio的问题还是很多~

    @Override
    public void onBackPressed() {
        ToastUtils.cancelToast();
        super.onBackPressed();
    }
在onBackPressed方法里面加上取消的代码可以增加用户体验。不会退出界面后还弹出toast

PS:只是一个思路,其实觉得用不上。默认的只有两种时间,退出app的时间间隔已经结束了

没找
public class MainActivity extends AppCompatActivity implements View.OnClickListener, OnTouchListener {
    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x111:
//                    Toast.makeText(MainActivity.this, "11", Toast.LENGTH_SHORT).show();
//                    methodlow();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button viewById = (Button) findViewById(R.id.bt_toast);
        Button button = (Button) findViewById(R.id.button);
        viewById.setOnClickListener(MainActivity.this);
        button.setOnClickListener(this);


 
  
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_toast:
//                Toast.makeText(MainActivity.this, "bt_toast", Toast.LENGTH_SHORT).show();
                ToastUtils.showToast(MainActivity.this, "点击测试");
                break;
            case R.id.button:
                ToastUtils.showToast(MainActivity.this, "buttonclick");


                break;
        }
    }


package com.tianch.test40.util;

import android.content.Context;
import android.widget.Toast;

/**
 * Created by Administrator on 2016/7/28.10
 */
public class ToastUtils {

    private static Toast toast;

    public static void showToast(Context context,
                                 String content) {
        if (toast == null) {
            toast = Toast.makeText(context,
                    content,
                    Toast.LENGTH_SHORT);
        } else {
            toast.setText(content);
        }
        toast.show();
    }

    public static void cancelToast() {
        if (toast != null) {
            toast.cancel();
        }
    }


}




你可能感兴趣的:(Android)