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
- afterTextChanged
- beforeTextChanged
- 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) {}
});
}
精彩评论