自定义View之字母导航控件

xml

<LinearLayout
     xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#FFF68F"
    >

    <EditText 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/round_edittext"
        />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       >
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:textSize="50sp"
            android:layout_gravity="center"
            android:gravity="center"
            android:id="@+id/mytextview"
            android:visibility="invisible"/>
        <com.example.uimyviews.MyView 
            android:layout_width="40dp"
            android:layout_height="match_parent"
            android:text="2"
            android:background="#ffffff"
            android:layout_gravity="right"
            android:id="@+id/myview"/>
    </FrameLayout>

</LinearLayout>

MainActivity

public class MainActivity extends Activity {

    TextView textView;
    MyView myView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) this.findViewById(R.id.mytextview);
        myView = (MyView) this.findViewById(R.id.myview);
        myView.setTextview(textView);

    }

}

MyView

package com.example.uimyviews;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class MyView extends View {

    int choose = -1;//选中哪块
    TextView textview;
    Paint p=new Paint();
    String[] str={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","#"};
    int width = -1;//view宽
    float height = -1;//view高
    float singleH = -1;//单个块的高
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    private void initPaint(){
        p.setColor(Color.BLACK);
        p.setTextSize(30);
        p.setAntiAlias(true);//抗锯齿
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        width = getWidth();
        height = getHeight();
        singleH = height/str.length;
        for(int i=0;i<str.length;i++){
            if(i != choose){
                canvas.drawText(str[i],((float)(width-p.measureText(str[i]))/2),singleH*(i+1),p);
            }
            else{
                p.setColor(Color.RED);
                canvas.drawText(str[i],((float)(width-p.measureText(str[i]))/2),singleH*(i+1),p);
                p.setColor(Color.BLACK);
            }

        }
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int e = event.getAction();
        float y = (float) event.getY();
        //  int÷float=int
        choose = (int)((y/height)*str.length);//选中的是第几个,【这个要小心】

        switch (e) {
        case MotionEvent.ACTION_UP:
            textview.setVisibility(View.INVISIBLE);//不可视
            choose = -1;
            invalidate();
            break;
        default://move and down
            if(choose >= 0 && choose < 27){
            textview.setText(str[choose]);
            textview.setVisibility(View.VISIBLE);//可视
            }
            invalidate();
            break;
        }
        return true;
    }


    public TextView getTextview() {
        return textview;
    }

    public void setTextview(TextView textview) {
        this.textview = textview;
    }
}

效果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值