首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

React Native -如何更改TextInput光标位置

要更改React Native中TextInput的光标位置,可以使用setSelectionRange方法

代码语言:javascript
复制
import React, { useRef } from 'react';
import { TextInput } from 'react-native';

const App = () => {
  const inputRef = useRef(null);

  const changeCursorPosition = (position) => {
    inputRef.current.setSelectionRange(position, position);
  };

  return (
    <>
      <TextInput
        ref={inputRef}
        style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
        onChangeText={(text) => console.log(text)}
      />
      <button onClick={() => changeCursorPosition(2)}>设置光标位置为2</button>
    </>
  );
};

export default App;

在这个例子中,光标位置设置为2。你可以根据需要更改changeCursorPosition函数中的参数来更改光标位置。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的视频

领券