锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 开源技术 / Java开源技术 / 支持选择和光标定位的定制旋转EditText
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

锐英源精品开源,禁止转载和任何形式的非法内容使用,违者必究


支持选择和光标定位的定制旋转EditText

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?

我想做的事

我想制作一个具有普通EditText视图的所有属性的旋转和翻转的EditText视图。

我的问题

我已经成功地(在SO用户的帮助下)制作了一个自定义的EditText视图,该视图既旋转又翻转。这是通过重写onDraw方法完成的。但是,光标和高亮显示都消失了,并且longtouch事件仍然指示原始文本位置。基本上,视图是重绘的,但触摸事件不是。

我如何获得触摸事件,高亮显示和光标也要旋转和翻转?

 

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

我读过的

带选择的EditText缩放(一个类似的问题,但并不完全相同。)

如何制作自定义Edittext,使其在android中旋转45度(@CommonsWare指出了一种解决方案,即需要通过触摸事件来完成其他工作。这是什么?)

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

 

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.我做了一个扩展EditText的自定义视图。在其中覆盖了onDraw方法以旋转和翻转画布。我覆盖了onMeasure,以使视图具有适合布局的尺寸。

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.

布局xml没有什么特别的。

(更新)这个问题是另一种尝试,但是最后我无法使它起作用:除了onDraw()之外,是否还需要覆盖invalidateDrawable()吗?

进一步说明

如果您想知道为什么我想旋转和翻转EditText视图,这就是原因。传统蒙古语在左右两列中垂直书写。与垂直镜像的蒙古字体结合使用时,将文本顺时针旋转90度并将其翻转会产生具有正确换行的可读输出。

这不是一个晦涩或孤立的问题。有数百万使用传统蒙古语的用户,但Android应用程序很少。其中,我还没有找到任何开源的。如果我可以使它正常工作,那么我想使代码对其他开发人员可用。

 

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

我现在正在看的地方

(更新)

我正在考虑View从头创建自定义视图(扩展)以创建类似的视图TextView。这TextView可以从应用程序进行更新,以像一个EditText视图。在这种情况下,我只需要使用常规字体将文本旋转90度,而无需翻转即可。但是,我必须自己做换行。

但是,在阅读@Chitrang的答案后,我认为我可以通过扩展TextView来做类似的事情。这样就可以避免自己做换行的麻烦。

图片更新

 

 

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.蒙古语从上到下,从左到右书写。现在,我正在使用此键盘在文本周围移动光标,但是我希望能够触摸屏幕将光标移动到某个位置。

 

Yeah listen bro, You are just rotating the view of EditText but if you go to the class and see the start end and bounds of EditText they will remain the same. so It is just drawing vertical, in memory is still the horizontal. There is a method setMovementMethod see this one and as they have added support RTL language you have to add support for vertical layout from extending TextView. see onTouch it's where cursor get bounds and appear on screen.是的,兄弟,您只是旋转EditText的视图,但是如果您转到类里,然后查看EditText的开始和范围,它们将保持不变。因此它只是垂直绘制,而在内存中仍然是水平绘制。有一种方法setMovementMethod可以看到这一点,因为它们已经添加了对RTL语言的支持,所以您必须通过扩展TextView添加对垂直布局的支持。参考onTouch,这是光标受限制并出现在屏幕上的地方

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,

试图通过一些较小的更改获得部分解决方案,请检查您是否要这样做。

1)在xml内的edittext声明中设置android:gravity =“ top | left”。

2)注意onDraw方法内部没有super方法调用,无法显示cursor。所以我调用

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 :) )对于触摸事件,我尝试交换x和y坐标。这样就可以使每个触摸事件具有光标。(刚尝试过,它就起作用了,很幸运:))

@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).注释onDraw方法中的这一行,以获取确切的触摸事件(通过反复试验发现)。

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上获得RTL语言支持,那么您只需要旋转它即可。但不幸的是,它无法与android正常工作。参考:如何在4.2之前的Android版本上处理RTL语言? 和Android设置与TextView希伯来语文本?

Things are much more simplier. Everything you need is already buit-in View class: http://developer.android.com/reference/android/view/View.html#setRotation(float) A bit more complicated solution if previous one would not works for you by some reason is: http://developer.android.com/reference/android/view/animation/RotateAnimation.html It also using for rotation of views (but animated in most cases however you could use it with zero transition duration).

Please let me know in the case you have some additional questions (guess no - there is self-explaing method and class).

Rotating just a canvas - you rotating only image on the screen. setRotation also handles all events, layout flow etc so it should works just fine in your case!

事情要简单得多。您需要的所有内容都已经内置在View类中:http : //developer.android.com/reference/android/view/View.html#setRotation(float) 如果以前的解决方案对某些人不起作用,则需要更复杂的解决方案原因是:http : //developer.android.com/reference/android/view/animation/RotateAnimation.html 它也用于旋转视图(但是在大多数情况下是动画的,但是可以使用零过渡时间)。

如果您还有其他问题,请让我知道(猜不出来-有自我解释的方法和类)。

仅旋转画布-您仅旋转屏幕上的图像。setRotation还可以处理所有事件,布局流程等,因此在您的情况下应该可以正常工作!

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.

我最终从零开始开发了一个垂直脚本MongolEditText。可作为mongol-library。的一部分使用

在这里,它与两个不同的第三方键盘一起使用。

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:

旧答案

这项工作仍在进行中,因此我不会将其标记为已解决,但请允许我发布到目前为止的内容。它完成了我想做的大部分事情。基本上,我使用TextView而不是EditText,因为EditText在旋转时会处理许多奇怪的事情。

我有一个不闪烁的光标,可以响应触摸事件,但是仍然不支持高亮显示。这是代码:

 

友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内