久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

android EditText ,keyboard textWatcher 問題

android EditText ,keyboard textWatcher problem(android EditText ,keyboard textWatcher 問題)
本文介紹了android EditText ,keyboard textWatcher 問題的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在開發一個 android 應用程序,并且我有一個 EditText,用戶可以在其中輸入數字.我想使用不同的貨幣格式(比如##、##、###)格式化數字,并且我想即時進行,即當用戶輸入每個數字時(而不是按下輸入時).我四處搜索,發現 TextWatcher 我第一次發現它很有希望,但是事實證明這是一種絕對的痛苦.我正在一臺只有軟鍵盤的 HTC Desire 手機上調試我的代碼.

I am working on a android app and I have an EditText where user can input numbers. I want to format the number using different currency formats (say ##,##,###) and I want to do it on the fly, ie when user enter each digit(not when enter is pressed). I googled around, and came across TextWatcher which I first found promising, but it turned out to be an absolute pain. I am debugging my code on a HTC Desire phone which only has a soft keyboard.

現在我想在用戶按下數字(0 到 9)、del(退格)鍵和 enter 鍵時獲得回調.從我的測試中我發現了這些(至少在我的手機上)

Now I want to get a callback when user press numbers (0 to 9) , del (backspace) key and enter key. From my testing I found these (atleast on my phone)

1) 調用editText onKeyListener當用戶按 del 或 enter 鍵時.當用戶按下回車鍵時,onKey一次輸入調用兩次函數(我相信這是 ACTION_UP 和ACTION_DOWN).當用戶按下 del 時,onKey 被調用一次(僅適用于ACTION_DOWN) 我不知道為什么.用戶時永遠不會調用 onKey按下任何數字(0 到 9),這也是我無法理解.

1) editText onKeyListener is called when user presses del or enter key. When user presses enter, onKey function is called twice for one enter (which I believe is for ACTION_UP and ACTION_DOWN). When user presses del, onKey is called once (only for ACTION_DOWN) which I dont know why. onKey is never called when user presses any digits(0 to 9) which too I cant understand.

2) TextWatchers 3個回調函數被稱為(在TextChanged之前,onTextChanged, afterTextChanged)每當用戶按下任何數字(0 到9) 鍵.所以我想通過使用TextWatcher 和 onKeyListener 在一起我可以得到我需要的所有回調.

2) TextWatchers 3 callback functions are called (beforeTextChanged, onTextChanged, afterTextChanged) whenever user presses any number (0 to 9) key . So I thought by using TextWatcher and onKeyListener together I can get all callbacks I need.

現在我的問題是這些..

Now my questions are these..

1) 首先在我的 HTC 軟鍵盤那里是一個鍵(帶有向下箭頭),當我點擊它時鍵盤辭職不給任何回調.我還是不敢相信android讓用戶編輯字段并辭職不讓程序處理(保存)編輯.現在我的editText 顯示一個值,而我的對象有另一個值(我正在保存用戶在輸入時進行編輯,并處理回通過重置按下鍵盤editText 值與中的值對象,但我對此沒有答案鍵盤向下鍵).

1) First in my HTC soft keyboard there is a key (a keyboard symbol with a down arrow) and when I click on it keyboard is resigned without giving any callback. I still cant believe android letting user to edit a field and resign without letting program to process (save) the edit. Now my editText is showing one value and my object has another value (I am saving user edits on enter, and handling back press on keyboard by reseting editText value with the value in the object , but I have no answer to this keyboard down key).

2) 二、我要格式化數字用戶輸入新數字后.說我已經有 123 在 editText 和用戶輸入按下 4,我想要我的editText 顯示 1,234.我吃飽了onTextChanged() 和afterTextChanged() 我可以格式化號碼并將其放回任何這些回調中的editText.我應該使用哪一個?哪一個是最佳實踐?

2) Second, I want to format the number after user entered the new digit. Say I already have 123 on editText and user entered pressed 4, I want my editText to display 1,234. I get full number on onTextChanged() and afterTextChanged() and I can format the number and put it back to editText in any of these callback. Which one should I use? Which is the best practice?

3) 第三個是最關鍵的問題.當應用程序啟動時,我把editText 中的當前對象值.假設我在 onResume() 上放了 123,當用戶輸入一個數字(比如 4)我想要它是 1234.但在我的 onTextChanged回調我得到的是4123.當我再按一個鍵(比如 5) 我是得到 45123.所以對于用戶輸入editText 光標指向末尾文本.但是當值由手,editText 光標似乎不是更新.我相信我必須做textWatcher 回調中的某些內容,但是我不知道我該怎么辦.

3) Third one is the most crucial problem. When app start I put the current object value in the editText. Say I put 123 on onResume(), and when user enter a digit (say 4) I want it to be 1234. But on my onTextChanged callback what I am getting is 4123. When I press one more key (say 5) I am getting 45123. So for user inputs editText cursor are pointing to end of the text. But when value is set by hand, editText cursor dont seems to be updating. I believe I have to do something in textWatcher callbacks but I dont know what I should do.

我在下面發布我的代碼.

I am posting my code below.

public class AppHome extends AppBaseActivity {
    private EditText ed = null;
    private NumberFormat amountFormatter = null;
    private boolean  isUserInput = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_home_screen);

        ed = (EditText)findViewById(R.id.main_amount_textfield);
        amountFormatter = new DecimalFormat("##,##,###");


        ed.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(event.getAction() == KeyEvent.ACTION_DOWN) 
                    return true;
                String strippedAmount = ed.getText().toString().replace(",", "");
                if(keyCode == KeyEvent.KEYCODE_DEL){
                    //delete pressed, strip number of comas and then delete least significant digit.
                    strippedAmount = strippedAmount.substring(0, strippedAmount.length() - 1);
                    int amountNumeral = 0;
                    try{
                        amountNumeral = Integer.parseInt(strippedAmount);
                    } catch(NumberFormatException e){
                    }
                    myObject.amount = amountNumeral;
                    isUserInput = false;
                    setFormattedAmount(amountNumeral,ed.getId());
                }else if(keyCode == KeyEvent.KEYCODE_ENTER){
                    //enter pressed, save edits and resign keyboard
                    int amountNumeral = 0;
                    try{
                        amountNumeral = Integer.parseInt(strippedAmount);
                    } catch(NumberFormatException e){
                    }
                    myObject.amount = amountNumeral;
                    isUserInput = false;
                    setFormattedAmount(myObject.amount,ed.getId());
                    //save edits
                    save();
                    //resign keyboard..
                    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    in.hideSoftInputFromWindow(AppHome.this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
                }
                return true;
            }
        });

        TextWatcher inputTextWatcher = new TextWatcher() {
            public void afterTextChanged(Editable s) { 
                if(isUserInput == false){
                    //textWatcher is recursive. When editText value is changed from code textWatcher callback gets called. So this variable acts as a flag which tells whether change is user generated or not..Possibly buggy code..:(
                    isUserInput = true;
                    return;
                }
                String strippedAmount = ed.getText().toString().replace(",", "");
                int amountNumeral = 0;
                try{
                    amountNumeral = Integer.parseInt(strippedAmount);
                } catch(NumberFormatException e){
                }
                isUserInput = false;
                setFormattedAmount(amountNumeral,ed.getId());
            }

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

        ed.addTextChangedListener(inputTextWatcher);
    }//end of onCreate...

    public void setFormattedAmount(Integer amount, Integer inputBoxId){
        double amountValue = 0;
        String textString =null;
        TextView amountInputBox = (TextView) findViewById(inputBoxId);

        amountValue = Double.parseDouble(Integer.toString(amount));
        textString = amountFormatter.format(amountValue).toString();
        amountInputBox.setText(textString);
    }
}

我知道這是一個大問題,但我正在處理同樣的問題 2 天.我是 android 新手,仍然無法相信沒有簡單的方法可以即時處理 textEdit 數據(我在 iphone 上輕松地做了同樣的事情).謝謝大家

I know it is a big question, but I am working on this same problem for 2 days. I am new to android and still cant believe that there is no easy way to process textEdit data on the fly (I done the same on iphone with ease). Thanks all

編輯:使用輸入過濾器后

InputFilter filter = new InputFilter() { 
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { 
            String strippedAmount = dest.toString() + source;
            strippedAmount = strippedAmount.replace(",", "");

        int amountNumeral = 0;
        try{
            amountNumeral = Integer.parseInt(strippedAmount);
        } catch(NumberFormatException e){
        }           
            return amountFormatter.format(amountNumeral).toString(); 
    } 
}; 

ed.setFilters(new InputFilter[]{filter}); 

當應用啟動時,我將 1,234 放在 editText 上

When app starts I am putting 1,234 on the editText

myObject.amount = 1234;
ed.setText(amountFormatter.format(myObject.amount).toString());

然后當用戶點擊editText時,鍵盤彈出,說用戶輸入數字6

Then when user clicks the editText, keyboard pops up, and say user enters digit 6

我得到:61234 我想要:12346

I am getting : 61234 I want : 12346

推薦答案

好吧,經過多次頭撞,我找到了解決光標位置問題的方法..我不知道這是否正確,但我得到了它的工作..

Well, after much head banging, I found a work around for cursor position problem..I dont know whether it is the correct way, But I got it working..

    TextWatcher inputTextWatcher = new TextWatcher() {
        public void afterTextChanged(Editable s) { 
            if(isUserInput == false){
                //textWatcher is recursive. When editText value is changed from code textWatcher callback gets called. So this variable acts as a flag which tells whether change is user generated or not..Possibly buggy code..:(
                isUserInput = true;
                ed.setSelection(ed.getText().length());
                return;
            }
            String strippedAmount = ed.getText().toString().replace(",", "");
            int amountNumeral = 0;
            try{
                amountNumeral = Integer.parseInt(strippedAmount);
            } catch(NumberFormatException e){
            }
            isUserInput = false;
            setFormattedAmount(amountNumeral,ed.getId());
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after){
        }
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    };
ed.addTextChangedListener(inputTextWatcher);


ed.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int length          =   ed.getText().length();
            ed.setCursorVisible(true);
            ed.setSelection(length);
        }
    });

ed.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
            if(event.getAction() == KeyEvent.ACTION_UP) 
                return true;
            String strippedAmount = ed.getText().toString().replace(",", "");
            if(keyCode == KeyEvent.KEYCODE_DEL){
                //delete pressed, strip number of comas and then delete least significant digit.
                strippedAmount = strippedAmount.substring(0, strippedAmount.length() - 1);
                int amountNumeral = 0;
                try{
                    amountNumeral = Integer.parseInt(strippedAmount);
                } catch(NumberFormatException e){
                }
                isUserInput = false;
                setFormattedAmount(amountNumeral,ed.getId());
                return true;
            }else if(keyCode == KeyEvent.KEYCODE_ENTER){
                //enter pressed, save edits and resign keyboard
                int amountNumeral = 0;
                try{
                    amountNumeral = Integer.parseInt(strippedAmount);
                } catch(NumberFormatException e){
                }
                isUserInput = false;
                setFormattedAmount(amountNumeral,ed.getId());
                //save edits
                //resign keyboard..
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                in.hideSoftInputFromWindow(AppHome.this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
                return true;
            }
            return false;
    }
});

我所做的是在editText的onClick()上,我強制將光標放在當前EditText文本的末尾,當用戶按下任何數字時我也做了同樣的事情.希望它對某人有所幫助..感謝所有試圖提供幫助的人.

What I have done is on onClick() of editText, I forcefully put the cursor at the end of the current EditText text, and I have done the same when user pressed any digit. Hope it helps someone..Thanks for everyone who tried to help.

這篇關于android EditText ,keyboard textWatcher 問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cut, copy, paste in android(在android中剪切、復制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數的數字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 一本大道久久a久久精二百 欧洲一区二区三区 | 视频一区二区三区在线观看 | 99精品99 | 久国产视频 | 99久久精品免费看国产四区 | 日韩欧美福利视频 | 亚洲美女一区二区三区 | 久久久国产亚洲精品 | 91se在线| 国产综合久久 | 亚洲国产成人精品女人久久久 | 午夜爽爽爽男女免费观看 | 欧美在线a | 国产清纯白嫩初高生在线播放视频 | 337p日本欧洲亚洲大胆鲁鲁 | 日韩高清中文字幕 | 中文字幕国产日韩 | 欧美精品一区二区三 | 国产精品成人国产乱一区 | 亚洲免费一区 | 黄色一级电影在线观看 | 欧美一区免费在线观看 | 黄色欧美大片 | 久久久久欧美 | 成人特区 | 国产特一级黄色片 | 成人在线免费视频 | 婷婷一级片 | 亚洲二区在线 | 国产美女高潮 | 亚洲美女一区 | 日日操日日舔 | 国产成人综合网 | 麻豆hd | 国产成人精品综合 | 九九伦理片 | 欧美mv日韩mv国产网站91进入 | 日本成人二区 | 国产精品久久久久久一区二区三区 | 精品国产一区二区三区久久狼黑人 | 午夜精品一区二区三区在线观看 |