![](/img/trans.png)
[英]EditText:how to change visibility once the user has done finished typing?
[英]Formating the text within an EditText box after the user has finished
我想在用戶完成后更改EditText框的內容。 假設用戶輸入了諸如50126057之類的數字,在他/她完成操作之后,我需要在用戶單擊應用程序上的“發送”按鈕之前或之后向其中添加一些字符。 它看起來應該像這樣:scn | 50126057 {),以便我在PC上的應用程序可以理解它。 無論如何,這可以做到嗎?
謝謝,非常感謝您的幫助
PS對不起,我的拼寫/語法
EditText word =(EditText)findViewById(R.id.username);
//內部按鈕單擊事件
//將在編輯文本中輸入的單詞分配給一個字符串變量(在本例中為'wordtoformat')
String wordtoformat=word.getText().toString();
//現在根據需要格式化單詞
wordtoformat="scn|"+wordtoformat+"{)";
//現在將格式化的字符串分配回編輯文本
word.setText(text);
希望它有用
您可以使用OnFocus偵聽器,如下所示:
EditText et = (EditText)findViewById(*editText id*);
et.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
String text = et.getText().ToString();
String newText = "scn|"+text+"{)";
et.setText(newText);
}
});
抱歉,如果我不是100%准確,但是我已經下班了,我這里沒有Android IDE :)
希望對您有幫助!
PS:僅當用戶失去EditText的焦點,然后單擊按鈕,或者單擊該按鈕用戶不會離開活動時,該用戶才能看到修改后的文本。 但是出於修改文本的目的,應該可以正常工作。
在點擊按鈕事件時,
btn.setOnClickListener(new onClickListener(){
public void onClick(View v){
String data="scn|"+edt.getText().toString()+"{)";
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.