pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

具有工作選擇、光標位置等的自定義旋轉 EditTe

Custom rotated EditText view with working selection, cursor location, etc(具有工作選擇、光標位置等的自定義旋轉 EditText 視圖)
本文介紹了具有工作選擇、光標位置等的自定義旋轉 EditText 視圖的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我想做什么

我想制作一個旋轉和翻轉的 EditText 視圖,它具有普通 EditText 視圖的所有屬性.

我的問題

我已經成功地(在 SO 用戶的幫助下)制作了一個自定義的 EditText 視圖,該視圖既可以旋轉又可以翻轉.這是通過覆蓋 onDraw 方法完成的.但是,光標和突出顯示消失了,longtouch 事件仍然指示原始文本位置.基本上,視圖被重繪,但觸摸事件沒有.

如何讓觸摸事件、突出顯示和光標也被旋轉和翻轉?

我讀到的

從頭開始.它作為

舊答案

這仍然是一項正在進行的工作,所以我不會將其標記為已解決,但讓我發布到目前為止的內容.它完成了我想做的大部分工作.基本上,我使用的是 TextView 而不是 EditText,因為 EditText 在旋轉時會做很多奇怪的事情.

我有一個不閃爍的光標來響應觸摸事件,但仍然不支持突出顯示.代碼如下:

public class MongolTextView extends TextView {私人TextPaint textPaint;私人油漆 cursorPaint = new Paint();私有布爾 mCursorIsVisible;私有 CursorTouchLocationListener 監聽器;//命名基于預先旋轉/鏡像的值私人浮動 mCursorBaseY;私人浮動 mCursorBottomY;私人浮動 mCursorAscentY;//這是一個負數私人浮動 mCursorX;私有靜態最終浮點數 CURSOR_THICKNESS = 2f;//構造函數public MongolTextView(上下文上下文,AttributeSet attrs,int defStyle){超級(上下文,屬性,defStyle);在里面();}public MongolTextView(上下文上下文,AttributeSet attrs){超級(上下文,屬性);在里面();}公共蒙古文本視圖(上下文上下文){超級(上下文);在里面();}//該類要求鏡像的蒙古文字體在 assets/fonts 文件夾中私人無效初始化(){//字體 tf = Typeface.createFromAsset(getContext().getAssets(),//字體/ChimeeWhiteMirrored.ttf");//setTypeface(tf);//使用上面注釋的代碼是在另一個應用程序中使用單一字體字體 tf = FontCache.get(SettingsActivity.FONT_DEFAULT, getContext());如果(tf!= null){設置字體(tf);}this.mCursorIsVisible = true;cursorPaint.setStrokeWidth(CURSOR_THICKNESS);cursorPaint.setColor(Color.BLACK);//TODO 應該和文本顏色一樣}//如果不需要觸摸功能,可以刪除此界面公共接口 CursorTouchLocationListener {/*** 返回用于光標的觸摸位置,以便您可以更新插入* 文本字符串中的位置.** @param 字形索引* 如果您使用的是* Unicode 字符串.*/公共無效 onCursorTouchLocationChanged(int glyphIndex);}@覆蓋受保護的無效onMeasure(int widthMeasureSpec,int heightMeasureSpec){//交換高度和寬度super.onMeasure(heightMeasureSpec, widthMeasureSpec);setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());}@覆蓋受保護的無效onDraw(帆布畫布){textPaint = getPaint();textPaint.setColor(getCurrentTextColor());textPaint.drawableState = getDrawableState();畫布.保存();//翻轉和旋轉畫布canvas.translate(getWidth(), 0);畫布.旋轉(90);canvas.translate(0, getWidth());canvas.scale(1, -1);canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());//繪制光標如果(mCursorIsVisible){canvas.drawLine(mCursorX, mCursorBottomY, mCursorX, mCursorBaseY + mCursorAscentY,游標畫);}getLayout().draw(畫布);畫布.restore();}公共無效showCursor(布爾可見){mCursorIsVisible = 可見;this.invalidate();//TODO 使光標閃爍}公共無效 setCursorColor(int 顏色) {cursorPaint.setColor(顏色);}公共無效 setCursorLocation(int characterOffset){布局布局 = this.getLayout();如果(布局!=空){嘗試 {//這個方法會導致很多崩潰,所以只是圍繞著//暫時嘗試 catchint line = layout.getLineForOffset(characterOffset);mCursorX = layout.getPrimaryHorizo??ntal(characterOffset);mCursorBaseY = layout.getLineBaseline(line);mCursorBottomY = layout.getLineBottom(line);mCursorAscentY = layout.getLineAscent(line);this.invalidate();} 捕捉(異常 e){e.printStackTrace();}}}公共類 InputWindowTouchListener 實現 OnTouchListener {@覆蓋public boolean onTouch(View view, MotionEvent 事件) {布局布局 = ((TextView) 視圖).getLayout();//為觸摸事件交換 x 和 yint y = (int) event.getX();int x = (int) event.getY();如果(布局!= null){int line = layout.getLineForVertical(y);int offset = layout.getOffsetForHorizo??ntal(line, x);mCursorX = layout.getPrimaryHorizo??ntal(offset);mCursorBaseY = layout.getLineBaseline(line);mCursorBottomY = layout.getLineBottom(line);mCursorAscentY = layout.getLineAscent(line);//mCursorHeightY = layout.getLineTop(line);view.invalidate();開關 (event.getAction()) {案例 MotionEvent.ACTION_DOWN://handler.postDelayed(mLongPressed, 1000);listener.onCursorTouchLocationChanged(offset);休息;案例 MotionEvent.ACTION_UP://handler.removeCallbacks(mLongPressed);//通知宿主活動新的光標位置休息;}}返回假;}}公共無效 setCursorTouchLocationListener(CursorTouchLocationListener 偵聽器){this.listener = 監聽器;}}

如果您有更好的內容,請隨時添加您自己的答案,或者如果您有任何要添加的內容以改進此問題(添加突出顯示、讓光標閃爍等),請隨時發表評論.這段代碼的最新版本應該在 github.

What I want to do

I want to make a rotated and flipped EditText view that has all of the properties of a normal EditText view.

My problem

I have successfully made (with much help from SO users) a custom EditText view that is both rotated and flipped. This was done by overriding the onDraw method. However, the cursor and highlighting are gone and a longtouch event still indicates the original text position. Basically, the view was redrawn but the touch events were not.

How do I get the touch events, highlighting, and cursor to also be rotated and flipped?

What I have read

EditText scale with selection (A similar problem but not quite the same.)

How to make a custom Edittext,so that it will look like as 45 degree rotated in android (@CommonsWare noted for one solution that addition work would need to be done with touch events. What is that work?)

http://developer.android.com/training/graphics/opengl/touch.html (Helpful, but I don't understand how to apply it in this situation.)

What I have tried

I made a custom view that extends EditText. In it overrode the onDraw method to rotate and flip the canvas. I overrode onMeasure to make the view have the right dimensions for the layout.

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.EditText;

public class MongolEditText extends EditText {

// Constructors
public MongolEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}
public MongolEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}
public MongolEditText(Context context) {
    super(context);
    init();
}

// This class requires the mirrored Mongolian font to be in the assets/fonts folder
private void init() {
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
            "fonts/MongolChagaanMirrored.ttf");
    setTypeface(tf);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(heightMeasureSpec, widthMeasureSpec);
    setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}

@Override
protected void onDraw(Canvas canvas) {
    TextPaint textPaint = getPaint();
    textPaint.setColor(getCurrentTextColor());
    textPaint.drawableState = getDrawableState();

    canvas.save();

    canvas.translate(getWidth(), 0);
    canvas.rotate(90);
    canvas.translate(0, getWidth());
    canvas.scale(1, -1);

    canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());

    getLayout().draw(canvas);
    canvas.restore();
}
}

There is nothing special for the layout xml.

(Update) This question is another attempt at it but in the end I couldn't get it to work: Does invalidateDrawable() need to be overridden in addition to onDraw()?

Further explanation

In case you are wondering why in the world I want to rotate and flip an EditText view, here is the reason. Traditional Mongolian is written vertically in left to right columns. In combination with a vertically mirrored Mongolian font, rotating the text 90 degrees clockwise and flipping it produces readable output with correct line wrapping.

This is not an obscure or isolated problem. There are millions of users of traditional Mongolian but very few Android apps. Of these, I haven't found any that are open source. If I can get this to work, I want to make the code available to other developers.

Where I am looking now

(update)

I was thinking about creating a custom view (that extends View) from scratch to create something like a TextView. This TextView could be updated from apps to act like an EditText view. In this case I would only need to rotate the text 90 degrees with a normal font but not flip it. However, I would have to do my own line wrapping.

However, after reading @Chitrang's answer I think I can do something similar by just extending a TextView. Then I can avoid the trouble of doing my own line wrapping.

Picture Update

Mongolian is written from top to bottom and left to right. Right now I am using this key pad to move a cursor around the text, but I would like to be able to touch the screen to move the cursor to a position.

解決方案

Update

I ended up developing a vertical script MongolEditText from scratch. It is available as a part of mongol-library.

Here it is being used with two different third party keyboards.

Old answer

This is still a work in progress so I won't mark this as solved yet, but let me post what I have so far. It does most of what I wanted to do. Basically, I'm using TextView rather than EditText because EditText was doing to many strange things when rotated.

I have an unblinking cursor that responds to touch events but highlighting is still not supported. Here is the code:

public class MongolTextView extends TextView {

    private TextPaint textPaint;
    private Paint cursorPaint = new Paint();
    private boolean mCursorIsVisible;
    private CursorTouchLocationListener listener;

    // Naming is based on pre-rotated/mirrored values
    private float mCursorBaseY;
    private float mCursorBottomY;
    private float mCursorAscentY; // This is a negative number
    private float mCursorX;
    
    private static final float CURSOR_THICKNESS = 2f; 

    // Constructors
    public MongolTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

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

    public MongolTextView(Context context) {
        super(context);
        init();
    }

    // This class requires the mirrored Mongolian font to be in the assets/fonts folder
    private void init() {
        //Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
        //      "fonts/ChimeeWhiteMirrored.ttf");
        //setTypeface(tf);
        
        // Use the above commented code is using a single font in another application 
        Typeface tf = FontCache.get(SettingsActivity.FONT_DEFAULT, getContext());
        if(tf != null) {
            setTypeface(tf);
        }

        this.mCursorIsVisible = true;

        cursorPaint.setStrokeWidth(CURSOR_THICKNESS);
        cursorPaint.setColor(Color.BLACK); // TODO should be same as text color
        
    }

    // This interface may be deleted if touch functionality is not needed
    public interface CursorTouchLocationListener {

        /**
         * Returns the touch location to be used for the cursor so you can update the insert
         * location in a text string.
         * 
         * @param glyphIndex
         *            You will need to translate glyphIndex into a Unicode index if you are using a
         *            Unicode string.
         */
        public void onCursorTouchLocationChanged(int glyphIndex);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // swap the height and width
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        textPaint = getPaint();
        textPaint.setColor(getCurrentTextColor());
        textPaint.drawableState = getDrawableState();

        canvas.save();

        // flip and rotate the canvas
        canvas.translate(getWidth(), 0);
        canvas.rotate(90);
        canvas.translate(0, getWidth());
        canvas.scale(1, -1);
        canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());

        // draw the cursor
        if (mCursorIsVisible) {
            canvas.drawLine(mCursorX, mCursorBottomY, mCursorX, mCursorBaseY + mCursorAscentY,
                    cursorPaint);
        }

        getLayout().draw(canvas);

        canvas.restore();
    }

    public void showCursor(boolean visible) {
        mCursorIsVisible = visible;
        this.invalidate();
        // TODO make the cursor blink
    }
    
    public void setCursorColor(int color) {
        cursorPaint.setColor(color);
    }

    public void setCursorLocation(int characterOffset) {
        
        Layout layout = this.getLayout();
        
        if (layout!=null){
            
            try {
                // This method is giving a lot of crashes so just surrounding with 
                // try catch for now
                
                int line = layout.getLineForOffset(characterOffset);
                mCursorX = layout.getPrimaryHorizontal(characterOffset);
                mCursorBaseY = layout.getLineBaseline(line);
                mCursorBottomY = layout.getLineBottom(line);
                mCursorAscentY = layout.getLineAscent(line);
                
                this.invalidate();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public class InputWindowTouchListener implements OnTouchListener {
        @Override
        public boolean onTouch(View view, MotionEvent event) {

            Layout layout = ((TextView) view).getLayout();

            // swapping x and y for touch events
            int y = (int) event.getX();
            int x = (int) event.getY();

            if (layout != null) {

                int line = layout.getLineForVertical(y);
                int offset = layout.getOffsetForHorizontal(line, x);

                mCursorX = layout.getPrimaryHorizontal(offset);
                mCursorBaseY = layout.getLineBaseline(line);
                mCursorBottomY = layout.getLineBottom(line);
                mCursorAscentY = layout.getLineAscent(line);
                //mCursorHeightY = layout.getLineTop(line);

                view.invalidate();
                
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    //handler.postDelayed(mLongPressed, 1000);
                    listener.onCursorTouchLocationChanged(offset);
                    break;
                case MotionEvent.ACTION_UP:
                    //handler.removeCallbacks(mLongPressed);
                    // notify the host activity of the new cursor location
                    
                    break;
                }

            }

            return false;
        }
        
    }
    
    public void setCursorTouchLocationListener(CursorTouchLocationListener listener) {
        this.listener = listener;
    }
}

Feel free to add your own answer if you have something better or make a comment if you have something to add for improving this (adding highlighting, getting the cursor to blink, etc.). The most recent version of this code should be on github.

這篇關于具有工作選擇、光標位置等的自定義旋轉 EditText 視圖的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cut, copy, paste in android(在android中剪切、復制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數的數字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 海峰资讯 - 专注装饰公司营销型网站建设和网络营销培训 | 塑胶地板-商用PVC地板-pvc地板革-安耐宝pvc塑胶地板厂家 | 济南保安公司加盟挂靠-亮剑国际安保服务集团总部-山东保安公司|济南保安培训学校 | 高空重型升降平台_高空液压举升平台_高空作业平台_移动式升降机-河南华鹰机械设备有限公司 | 家庭教育吧-在线家庭教育平台,专注青少年家庭教育 | 沈阳激光机-沈阳喷码机-沈阳光纤激光打标机-沈阳co2激光打标机 | 定制防伪标签_防伪标签印刷_防伪标签厂家-510品保防伪网 | 马尔表面粗糙度仪-MAHR-T500Hommel-Mitutoyo粗糙度仪-笃挚仪器 | 数显恒温油浴-电砂浴-高温油浴振荡器-常州迈科诺仪器有限公司 | 手术室净化厂家-成都做医院净化工程的公司-四川华锐-15年特殊科室建设经验 | 建大仁科-温湿度变送器|温湿度传感器|温湿度记录仪_厂家_价格-山东仁科 | 深圳市索富通实业有限公司-可燃气体报警器 | 可燃气体探测器 | 气体检测仪 | 柔软云母板-硬质-水位计云母片组件-首页-武汉长丰云母绝缘材料有限公司 | 山东螺杆空压机,烟台空压机,烟台开山空压机-烟台开山机电设备有限公司 | 无轨电动平车_轨道平车_蓄电池电动平车★尽在新乡百特智能转运设备有限公司 | 焊锡丝|焊锡条|无铅锡条|无铅锡丝|无铅焊锡线|低温锡膏-深圳市川崎锡业科技有限公司 | 精密冲床,高速冲床等冲压设备生产商-常州晋志德压力机厂 | 医疗仪器模块 健康一体机 多参数监护仪 智慧医疗仪器方案定制 血氧监护 心电监护 -朗锐慧康 | 【甲方装饰】合肥工装公司-合肥装修设计公司,专业从事安徽办公室、店面、售楼部、餐饮店、厂房装修设计服务 | 烘箱-工业烘箱-工业电炉-实验室干燥箱 - 苏州华洁烘箱制造有限公司 | 交流伺服电机|直流伺服|伺服驱动器|伺服电机-深圳市华科星电气有限公司 | 多功能真空滤油机_润滑油全自动滤油机_高效真空滤油机价格-重庆润华通驰 | 医院专用门厂家报价-医用病房门尺寸大全-抗菌木门品牌推荐 | 恒压供水控制柜|无负压|一体化泵站控制柜|PLC远程调试|MCGS触摸屏|自动控制方案-联致自控设备 | 耐火浇注料价格-高强高铝-刚玉碳化硅耐磨浇注料厂家【直销】 | 体检车_移动CT车_CT检查车_CT车_深圳市艾克瑞电气有限公司移动CT体检车厂家-深圳市艾克瑞电气有限公司 | 超声波焊接机,振动摩擦焊接机,激光塑料焊接机,超声波焊接模具工装-德召尼克(常州)焊接科技有限公司 | 仿古建筑设计-仿古建筑施工-仿古建筑公司-汉匠古建筑设计院 | 翰香原枣子坊加盟费多少钱-正宗枣核糕配方培训利润高飘香 | 干粉砂浆设备_干混砂浆生产线_腻子粉加工设备_石膏抹灰砂浆生产成套设备厂家_干粉混合设备_砂子烘干机--郑州铭将机械设备有限公司 | 数控车床-立式加工中心-多功能机床-小型车床-山东临沂金星机床有限公司 | 北京发电车出租-发电机租赁公司-柴油发电机厂家 - 北京明旺盛安机电设备有限公司 | 球磨机,节能球磨机价格,水泥球磨机厂家,粉煤灰球磨机-吉宏机械制造有限公司 | 合肥网带炉_安徽箱式炉_钟罩炉-合肥品炙装备科技有限公司 | 东莞市踏板石餐饮管理有限公司_正宗桂林米粉_正宗桂林米粉加盟_桂林米粉加盟费-东莞市棒子桂林米粉 | 陶瓷砂磨机,盘式砂磨机,棒销式砂磨机-无锡市少宏粉体科技有限公司 | 西门子代理商_西门子变频器总代理-翰粤百科| 筒瓦厂家-仿古瓦-寺庙-古建琉璃瓦-宜兴市古典园林建筑陶瓷厂有限公司 | 3d可视化建模_三维展示_产品3d互动数字营销_三维动画制作_3D虚拟商城 【商迪3D】三维展示服务商 广东健伦体育发展有限公司-体育工程配套及销售运动器材的体育用品服务商 | 爱佩恒温恒湿测试箱|高低温实验箱|高低温冲击试验箱|冷热冲击试验箱-您身边的模拟环境试验设备技术专家-合作热线:400-6727-800-广东爱佩试验设备有限公司 | 浙江上沪阀门有限公司|