开发者

dollar symbol in edittext on android

开发者 https://www.devze.com 2023-03-18 20:27 出处:网络
in my app,in开发者_如何学Python a EditText i have to set dollar sign permanently , always typing is start after the dollar symbol and cannot be deleted. how to do that

in my app,in开发者_如何学Python a EditText i have to set dollar sign permanently , always typing is start after the dollar symbol and cannot be deleted. how to do that .

for example

      rate : $10.00

i use 

  android:hint="$" 

it is deleted when cursor is invoked. my need is $ symbol always present and cannot be deleted. it to fixed. how to get that? please assist me.


You can set TextWatcher via addTextChangedListener. There are 3 callbacks

  1. afterTextChanged
  2. beforeTextChanged
  3. onTextChanged

in TextWatcher. You can play with them to keep the "$" permanently. The first thing that came to my mind is to check if the new string that is typed is empty to replace it is "$".


You could implement TextWatcher and add it for your EditText. In this watcher you cal add $ to string entered by user.


Just add an onChange listener and insert the $ after the user is done input.

private EditText yourEditText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    yourEditText = (EditText) findViewById(R.id.yourEditTextId);

    yourEditText.addTextChangedListener(new TextWatcher() {
      @Override
      public void afterTextChanged(Editable s) {
        yourEditText.setText("$" + yourEditText.getText().toString());
      }
      @Override
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {}
   });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消