定制旋转的EditText视图的工作选择,光标位置等 [英] Custom rotated EditText view with working selection, cursor location, etc

查看:371
本文介绍了定制旋转的EditText视图的工作选择,光标位置等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做什么

我想打一个旋转和翻转的EditText认为拥有所有正常的EditText视图的属性。

我的问题

我已经成功地(从这样用户太多的帮助)的自定义的EditText观点,既旋转和翻转。这是通过重写OnDraw的方法完成。但是,光标并高亮显示都不见了和longtouch事件仍表示原文位置。基本上,观点重绘,但触摸事件不是。

我如何得到触摸事件,突出显示,并且光标也可以旋转和翻转?

我读

的EditText与选择规模(类似的问题,但不完全一样。)

<一个href="http://stackoverflow.com/questions/11105207/how-to-make-a-custom-edittext-so-that-it-will-look-like-as-45-degree-rotated-in">How使一个自定义EDITTEXT,使得它看起来像在机器人的45度旋转(@CommonsWare注意到一个解决方案,除了工作将需要与触摸事件完成的。那是什么工作?)

http://developer.android.com/training/graphics/opengl/ touch.html (有益的,但​​我不知道如何将它应用在这种情况下。)

我已经试过

我做了延伸的EditText自定义视图。在它覆盖了OnDraw的方法来旋转和翻转画布。我推翻onMeasure,使视图对布局的正确尺寸。

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

公共类MongolEditText扩展的EditText {

//构造函数
公共MongolEditText(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);
    在里面();
}
公共MongolEditText(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
    在里面();
}
公共MongolEditText(上下文的背景下){
    超(上下文);
    在里面();
}

//这个类需要镜像蒙古字体是在资产/ fonts文件夹
私人无效的init(){
    字样TF = Typeface.createFromAsset(的getContext()。getAssets()
            字体/ MongolChagaanMirrored.ttf);
    setTypeface(TF);
}

@覆盖
保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
    super.onMeasure(heightMeasureSpec,widthMeasureSpec);
    setMeasuredDimension(getMeasuredHeight(),getMeasuredWidth());
}

@覆盖
保护无效的OnDraw(帆布油画){
    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()画(油画);
    canvas.restore();
}
}
 

没有什么特别的布局XML。

(更新)这个问题是它另一种尝试,但最终我无法得到它的工作:<一href="http://stackoverflow.com/questions/25479326/does-invalidatedrawable-need-to-be-overridden-in-addition-to-ondraw">Does invalidateDrawable()需要被除的OnDraw()?

覆盖

进一步解释

如果你想知道为什么在这个世界上我想旋转和翻转一个EditText来看,这里就是这个原因。传统的蒙古垂直写在左到右列。在同一个垂直镜像蒙字体组合,顺时针旋转90度的文本和翻转它产生具有正确的换行可读的输出。

这是不是一个不起眼的或孤立的问题。还有数以百万计的蒙古族传统却很少Android应用程序的用户。其中,我还没有发现任何都是开源的。如果我能得到这个工作,我想使code提供给其他开发商。

如果你不知道自己的答案,但你要帮我找到一个,那么请upvote的问题,使更多的人会看到它。

感谢您的帮助。

如果我现在看

(更新)

我在想从头开始创建自定义视图(扩展查看)创造的东西就像一个的TextView 。这的TextView ,可以从应用程序的更新,像一个的EditText 视图。在这种情况下,我只需要一个普通字体旋转文字90度,但不能将其翻转。不过,我会做我自己换行。

不过,看完@ Chitrang的回答后,我想我能做到,只需伸出一个TextView类似的东西。然后,我能避免做我自己换线的麻烦。

照片更新

蒙古人写入从上到下和从左写。现在我用这个键盘来移动文本光标,但我想能够触摸屏幕,将光标移动到的位置。

解决方案

我试着去与小的改动的部分解决方案,是否这就是你想要的。

1):设置机器人:重力=顶|留里面的xml您的EditText声明

2)注意到没有方法调用的OnDraw方法中,不能够显示光标。 于是我打电话,

  super.onDraw(画布);
 

相反,

  getLayout()画(油画)。
 

3)对于触摸事件,我想交换x和y坐标。 所以,你可以有光标按触摸事件。(刚试过,这是工作,很幸运:))

  @覆盖
公共布尔的onTouchEvent(MotionEvent事件){
    // TODO自动生成方法存根
    event.setLocation(event.getY(),event.getX());
    返回super.onTouchEvent(事件);
}
 

评论此行的OnDraw方法中,对于确切的触摸事件(通过反复试验找到)。

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

4)我不能做关于突出或选择文字的任何事情。

),另一种解决方法:如果你认为,一些如何你RTL语言支持 在EditText上工作,那么你只需要旋转它。但不幸的是它不能正常工作与Android。 参考:<一href="http://stackoverflow.com/questions/15746091/how-to-handle-rtl-languages-on-$p$p-4-2-versions-of-android">How处理RTL语言上pre 4.2版本的Andr​​oid? 和<一href="http://stackoverflow.com/questions/6302221/android-setting-with-textview-for-hebrew-text/6302325#6302325">Android与TextView的希伯来语文本设置?

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.

If you don't know the answer yourself, but you want to help me find one, then please upvote the question so that more people will see it.

Thank you for your help.

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 write. 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.

解决方案

Tried to get the partial solution with minor changes, check if that's you want.

1) Set android:gravity="top|left" in your edittext declaration inside xml.

2) Noticed that without super method call inside onDraw method, not able to show cursor. So I called,

super.onDraw(canvas);

instead,

getLayout().draw(canvas);

3) For touch events, I tried to swap x and y coordinates. So that you can have cursor as per touch event.(Just tried and it was working, got lucky :) )

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    event.setLocation(event.getY(), event.getX());
    return super.onTouchEvent(event);
}

Comment this line inside onDraw method, for exact touch event(Found by trial and error).

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

4) I could not do any thing about highlighting or selecting text.

?) Another Solution : If you think that, some how you get RTL language support work on edittext then you just need to rotate it. But unfortunately its not working properly with android. Reference : How to handle RTL languages on pre 4.2 versions of Android? and Android setting with TextView for Hebrew text?

这篇关于定制旋转的EditText视图的工作选择,光标位置等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆