簡體   English   中英

React Native 文本輸入如何添加到數值中?

[英]How can a react native textinput be additive for numeric values?

我正在開發一個從用戶那里獲取號碼的移動應用程序。 我想知道如何使該 textinput 以附加方式接受用戶輸入(數字),以便當用戶單擊 5 兩次時,textInput 將輸入接受為 10 而不是 55

我能夠實現我需要的文本輸入。 我的目標是使用帶有按鈕 1、2、5、10、20、50、100 和 200 的自定義鍵盤,以便當用戶單擊 10 然后單擊 5 時,textinput 顯示 15 而不是 105,因為普通鍵盤和 textinput 會表現.

const CustomInput = () => {
  const [textValue, setTextValue] = useState();
  const [value, setValue] = useState('');
  const [input, setInput] = useState(false);
  const [lastInput, setLastInput] = useState(0);

  const onChangeText = text => {
    let newValue;
    setTextValue(text);

    if (value == '') {
      newValue = Number(text);
    } else {
      newValue = Number(value) + Number(text);
    }
    setValue(newValue);
    setTextValue('');
  };

  return (
    <View style={[styles.container, {marginTop: 40, paddingVertical: 70}]}>
      <CustomTextInput
        placeholder={value !== '' ? `${value}` : 'Enter a number'}
        placeholderTextColor={value !== '' ? 'black' : '#0009'}
        value={textValue}
        customKeyboardType="price"
        onChangeText={val => onChangeText(val)}
        style={styles.input}
      />
      <Text style={{color: 'black'}}>text: {textValue}</Text>
      <Text style={{color: 'black'}}>Price: {value}</Text>
    </View>
  );
};

您可以嘗試這種方法。

   const onTextInput = (value) => {
        let savedValue;
        if (savedValue == value) {
          savedValue = savedValue + value;
          performCalculation(savedValue);
        } else {
          savedValue = value;
          performCalculation(savedValue);
        }
      };

 const TypeHere = () => {

  return (
    <View style={{padding: 10}}>
      <TextInput
        style={{height: 40}}
        placeholder="Type here!"
      onChangeText={text => onTextInput(text)}
      />
    </View>
  );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM