当前位置: 代码迷 >> 综合 >> Toast 自定义显示位置,toast和通讯录的a-z 一起走 部分代码
  详细解决方案

Toast 自定义显示位置,toast和通讯录的a-z 一起走 部分代码

热度:75   发布时间:2023-12-16 16:31:45.0
Toast的显示位置可以我们自己设置,通过如下命令:
toast = Toast.makeText(getContext(), "" + (char) ('A' + position),Toast.LENGTH_SHORT);
//可以控制toast显示的位置
toast.setGravity(Gravity.LEFT, xoffset, yoffset);
toast.show();

同时我们通过toast.cancle()可以用来取消一个toast,这在我们滑动到下一个字母的时候,就可以取消掉上一个字母的显示。
if(toast!=null)toast.cancel();


package com.zqxue.Review151222;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**  * Created by XUE on 2015/12/22.  */ public class SimpleView extends LinearLayout {private Paint paint;
    private float stopY;
    private Toast toast;

    public SimpleView(Context context) {super(context);
    }public SimpleView(Context context, AttributeSet attrs) {super(context, attrs);
        init(context,attrs);

    }public void init(Context context,AttributeSet attrs){paint = new Paint();
        paint.setColor(Color.RED);

        for (int i = 0; i < 26; i++) {TextView textView = new TextView(context);
            String text = Character.toString((char)('A'+i));

            textView.setText(text);
            ViewGroup.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    0,
                    1
            );
            textView.setLayoutParams(layoutParams);

            //textSize
            int height = getHeight();
            int textSize = height/26-2;

            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
            textView.setGravity(Gravity.RIGHT);
            addView(textView);
        }}private int currentPosition=-1;
    @Override
    public boolean onTouchEvent(MotionEvent event) {boolean ret = false;
        int action = event.getAction();
        float ex = event.getX();
        float ey = event.getY();
        stopY=ey;
        int position = -1;
        String type = "";
        switch (action){case MotionEvent.ACTION_DOWN:type ="DOWN";
                currentPosition=-1;
                //toast  if(toast!=null)toast.cancel();toast = Toast.makeText(getContext(), "" + (char) ('A' + position),Toast.LENGTH_SHORT);Log.d("151222MY","LONG="+Toast.LENGTH_LONG+" SHORT="+Toast.LENGTH_SHORT+ ex+""+ey);//可以控制toast显示的位置//toast.setGravity(Gravity.LEFT, right + child.getWidth() + 200, bottom - getHeight() / 2);toast.setGravity(Gravity.LEFT, getWidth() - 150, (int) ey - getHeight() / 2);toast.show();
                ret=true;
                break;
            case MotionEvent.ACTION_MOVE:int childCount = getChildCount();
                for (int i = 0; i < childCount; i++) {View child = getChildAt(i);
                    int top = child.getTop();
                    int left = child.getLeft();
                    int right = child.getRight();
                    int bottom = child.getBottom();
                    if(top<=ey&&left<=ex){if(right>=ex&&bottom>=ey){position = i;
                        }}if(position>-1&&currentPosition!=position){Log.d("151222MY","click "+position);
                        Log.d("151222MY","click"+(char)('A'+position));
                        currentPosition=position;
                        if(toast!=null)toast.cancel();toast = Toast.makeText(getContext(), "" + (char) ('A' + position),Toast.LENGTH_SHORT);Log.d("151222MY","LONG="+Toast.LENGTH_LONG+" SHORT="+Toast.LENGTH_SHORT);//可以控制toast显示的位置//toast.setGravity(Gravity.LEFT, right + child.getWidth() + 200, bottom - getHeight() / 2);toast.setGravity(Gravity.LEFT, getWidth()-150, (int)ey - getHeight() / 2);toast.show();
                    }}type="MOVE";
                break;
            case MotionEvent.ACTION_UP:type="UP";
                break;
        }//Log.d("151222MY", type);

        return ret;
    }@Override
    protected void onDraw(Canvas canvas) {super.onDraw(canvas);
        canvas.drawLine(0, 0, 300, stopY, paint);
        Log.d("151222MY","onDraw");
    }
}
  相关解决方案