注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

沉默蜂B4A安卓编程

国内资料最全的B4A资料库

 
 
 

日志

 
 

编辑框中文本选中操作  

2017-02-25 12:36:16|  分类: 文本操作 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |


Subject: Routines for selecting text and cursor management.

The result of these routines is visible only if the EditText has the focus !

Reminder of existing methods:

EditText.RequestFocus : Tries to set the focus to this view. Returns True if the focus was set.
EditText.SelectAll : Selects the entire text of EditText
EditText.SelectionStart : Gets or sets the selection start position (or the cursor position).
Returns -1 if there is no selection or cursor.

Sub: setCursorVisible 
Description: Sets the cursor visible or hides it.
Code:
'Sets the cursor visible or hides it
Sub setCursorVisible(edt As EditText, Visible As Boolean)
    
Dim jo = edt As JavaObject
    jo.RunMethod(
"setCursorVisible"Array As Object(Visible))
End Sub
Sub: setTextIsSelectable 
Description: Sets the text selectable or not selectable.
Code:
'Sets the text selectable or not selectable
Sub setTextIsSelectable(edt As EditText, Selectable As Boolean)
    
Dim jo = edt As JavaObject
    jo.RunMethod(
"setTextIsSelectable"Array As Object(Selectable))
End Sub
Sub: setSelection 
Description: Selects the text between the two indexes.
Code:
'Selects the text between the two indexes.
Sub setSelection(edt As EditText, StartIndex As Int, EndIndex As Int)
    
Dim jo = edt As JavaObject
    jo.RunMethod(
"setSelection"Array As Object(StartIndex, EndIndex))
End Sub
Sub: getSelectionStart
Description: Returns selection start index.
Code:
'Gets the selection start index
Sub getSelectionStart(edt As EditTextAs Int
    
Dim jo = edt As JavaObject
    jo.RunMethod(
"getSelectionStart"Null)
End Sub
Sub: getSelectionEnd
Description: Returns the selection end index.
Code:
'Gets the selection end index
Sub getSelectionEnd(edt As EditTextAs Int
    
Dim jo = edt As JavaObject
    jo.RunMethod(
"getSelectionEnd"Null)
End Sub
Sub: CopyToClipboard
Description: Copies the selected text in the given EditText view to the clipboard.
Needs the Clipboard library.
Dim clip As BClipboard can be moved from the routine to Globals.
Code:
'Copies the selected text from the given EditText view to the Clipboard
Sub CopyToClipboard(edt As EditText)
    
Dim txt As String
    
Dim i1, i2 As Int
    
Dim clip As BClipboard
    
Dim jo = edt As JavaObject
    i1 = jo.RunMethod(
"getSelectionStart"Null)
    i2 = jo.RunMethod(
"getSelectionEnd"Null)
    txt = edt.Text.SubString2(i1, i2)
    clip.setText(txt)
End Sub

The JavaObject routines can also be used without a sub like below or like in the CopyToClipboard routine above:
Code:
Dim jo = edt As JavaObject

jo.RunMethod(
"setTextIsSelectable"Array As Object(True))
jo.RunMethod(
"setCursorVisible"Array As Object(True))
jo.RunMethod(
"setSelection"Array As Object(510))
Dependencies: JavaObject library, Clipboard library (only for CopyToClipboard)

Tags: EditText, selection, setSelection, text, setTextIsSelectable, cursor, setCursorVisible, copy, clipboard

Attached a small test program.

EDIT: 2014.08.04
Added the getSelectionStart, getSelectionEnd and CopyToClipboard routines.

EDIT: 2016.06.27
Amended two errors. The methods getSelectionStart and getSelectionEnd didn't return a value.



============================================================
官网例程源码


#Region  Project Attributes 
#ApplicationLabel: EditTextUtils
#VersionCode: 1
#VersionName: 
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals

End Sub

Sub Globals
Dim edtTest, edtTest2 As EditText
Dim txt As String
Dim clip As BClipboard
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")
txt = "1 Long text" & CRLF
txt = txt & "2 Long text" & CRLF
txt = txt & "3 Long text" & CRLF
txt = txt & "4 Long text" & CRLF
txt = txt & "5 Long text"
edtTest.Text = txt
edtTest.RequestFocus
setTextIsSelectable(edtTest, True)
setCursorVisible(edtTest, True)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnGoToStart_Click
edtTest.SelectionStart = 0
End Sub

Sub btnGoToEnd_Click
edtTest.SelectionStart = edtTest.Text.Length
End Sub

Sub btnSelectAll_Click
edtTest.RequestFocus
edtTest.SelectAll
End Sub

Sub btnSetSelection_Click
Dim c As Int
edtTest.RequestFocus
setSelection(edtTest, 5, 10)
c = getSelectionEnd(edtTest)
End Sub

'copies the selected text from the given EditText view to the Clipboard
Sub btnCopyToClipboard_Click
CopyToClipboard(edtTest)
End Sub

'Copies the selected text from the given EditText view to the Clipboard
Sub CopyToClipboard(edt As EditText)
Dim txt As String
Dim i1, i2 As Int
' Dim clip As BClipboard
Dim jo = edt As JavaObject
i1 = jo.RunMethod("getSelectionStart", Null)
i2 = jo.RunMethod("getSelectionEnd", Null)
txt = edt.Text.SubString2(i1, i2)
clip.setText(txt)
End Sub

'Gets the selection start index
Sub getSelectionStart(edt As EditText) As Int
Dim jo = edt As JavaObject
Return jo.RunMethod("getSelectionStart", Null)
End Sub

'Gets the selection end index
Sub getSelectionEnd(edt As EditText) As Int
Dim jo = edt As JavaObject
Return jo.RunMethod("getSelectionEnd", Null)
End Sub

'Selects the text between the two indexes.
Sub setSelection(edt As EditText, StartIndex As Int, EndIndex As Int)
Dim jo = edt As JavaObject
jo.RunMethod("setSelection", Array As Object(StartIndex, EndIndex))
End Sub

'Sets the cursor visible or hides it
Sub setCursorVisible(edt As EditText, Visible As Boolean)
Dim jo = edt As JavaObject
jo.RunMethod("setCursorVisible", Array As Object(Visible))
End Sub

'Sets the text selectable or not selectable
Sub setTextIsSelectable(edt As EditText, Selectable As Boolean)
Dim jo = edt As JavaObject
jo.RunMethod("setTextIsSelectable", Array As Object(Selectable))
End Sub

  评论这张
 
阅读(45)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017