Android $符号应根据字符串输入移动

Android $符号应根据字符串输入移动,android,android-edittext,Android,Android Edittext,我有一个EditText,当我输入任何数字时,$符号应该根据字符串输入移动。例如,如果我输入20,$符号应该向右移动,它应该是20$。如果我输入200,$符号应该向右移动,它应该是200$。任何帮助都将不胜感激 以下是我的代码目前的样子:` <EditText android:id="@+id/amount_edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android

我有一个EditText,当我输入任何数字时,$符号应该根据字符串输入移动。例如,如果我输入20,$符号应该向右移动,它应该是20$。如果我输入200,$符号应该向右移动,它应该是200$。任何帮助都将不胜感激

以下是我的代码目前的样子:`

<EditText
android:id="@+id/amount_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" 
android:layout_marginTop="@dimen/space_normal"
app1:inputTextSize="@dimen/text_large" 
app1:inputIconDrawable="@drawable/ic_dollar_sign"
app1:inputIconTint="@color/colorDark"
app1:inputOneHint="@string/offer_amount"            
app1:inputIconTranslateY="@dimen/offer_large_text_icon_shift"
app1:inputType="number"/>


`使用
添加TextChangedListener(TextWatcher watcher)
编辑文本中添加
TextWatcher

实现
TextWatcher.onTextChanged(字符序列,int start,int before,int count)
如下:

EditText amountEditText = (EditText) findViewById(R.id.amount_edit_text);
amountEditText.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {
   }

   public void beforeTextChanged(CharSequence s, int start, int count, int after) {
   }

   public void onTextChanged(CharSequence s, int start, int before, int count) {
        // add $ only if we need to
        if (!s.toString().substring(s.length() - 1)).equals("$")) {
            amountEditText.setText(s + "$");
        }
   }
});
TextWatcher.onTextChanged(字符序列、int-start、int-before、int-count){
EditText.setText(s+“$”);
}

您可以使用它来监视文本何时更改。然后,您可以将$附加到任何您想要的位置

编辑:正如Cruncher所指出的,这可能引发另一个ContextChanged事件。因此,将它包装在if中,检查它是否已经以$结尾

大概是这样的:

EditText amountEditText = (EditText) findViewById(R.id.amount_edit_text);
amountEditText.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {
   }

   public void beforeTextChanged(CharSequence s, int start, int count, int after) {
   }

   public void onTextChanged(CharSequence s, int start, int before, int count) {
        // add $ only if we need to
        if (!s.toString().substring(s.length() - 1)).equals("$")) {
            amountEditText.setText(s + "$");
        }
   }
});

你试了什么?这只是一个语义问题,但美元符号通常不在数字的左边吗?e、 g$200@RayfenWindspear:是的,但是,把它放在右边才是要求。嗯,在textwatcher中更改文本不会触发另一个ContextChanged吗?