TextView的textIsSelectable属性和setMovementMethod()
TextView的textIsSelectable属性和setMovementMethod()
TextView的textIsSelectable属性可以支持长按文字可以复制,搜索等,而且支持对TextView的内容滑动。具体见图片
TextView的setMovementMethod()方法,也可以支持对TextView的内容滑动,但对Textview内容不支持长按文字可以复制,搜索等。
public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TextView view = (TextView) findViewById(R.id.textview);view.setMovementMethod(ScrollingMovementMethod.getInstance());}
}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"><TextView
android:id="@+id/textview"android:layout_width="match_parent"android:layout_height="wrap_content"android:maxHeight="200dp"android:text="曲曲折折的荷塘上面,弥望⑷的是田田⑸的叶子。叶子出水很高,像亭亭的舞女的裙。层层的叶子中间,零星地点缀着些白花,有袅娜⑹地开着的,有羞涩地打着朵儿的;正如一粒粒的明珠,又如碧天里的星星,又如刚出浴的美人。微风过处,送来缕缕清香,仿佛远处高楼上渺茫的歌声似的。这时候叶子与花也有一丝的颤动,像闪电般,霎时传过荷塘的那边去了。叶子本是肩并肩密密地挨着,这便宛然有了一道凝碧的波痕。叶子底下是脉脉⑺的流水,遮住了,不能见一些颜色;而叶子却更见风致⑻了。月光如流水一般,静静地泻在这一片叶子和花上。薄薄的青雾浮起在荷塘里。叶子和花仿佛在牛乳中洗过一样;又像笼着轻纱的梦。虽然是满月,天上却有一层淡淡的云,所以不能朗照;但我以为这恰是到了好处——酣眠固不可少,小睡也别有风味的。月光是隔了树照过来的,高处丛生的灌木,落下参差的斑驳的黑影,峭楞楞如鬼一般;弯弯的杨柳的稀疏的倩影,却又像是画在荷叶上。塘中的月色并不均匀;但光与影有着和谐的旋律,如梵婀玲⑼上奏着的名曲。"android:textIsSelectable="true" />RelativeLayout>- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
下面是setTextIsSelectable源码,实际上调用的也是这个setMovementMethod()方法
public void setTextIsSelectable(boolean selectable) {if (!selectable && mEditor == null) return; // false is default value with no edit datacreateEditorIfNeeded();if (mEditor.mTextIsSelectable == selectable) return;mEditor.mTextIsSelectable = selectable;setFocusableInTouchMode(selectable);setFocusable(selectable);setClickable(selectable);setLongClickable(selectable);// mInputType should already be EditorInfo.TYPE_NULL and mInput should be nullsetMovementMethod(selectable ? ArrowKeyMovementMethod.getInstance() : null);setText(mText, selectable ? BufferType.SPANNABLE : BufferType.NORMAL);// Called by setText above, but safer in case of future code changesmEditor.prepareCursorControllers();}- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
