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

android EditText ,keyboard textWatcher 問題

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

問題描述

限時(shí)送ChatGPT賬號(hào)..

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

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.

現(xiàn)在我想在用戶按下數(shù)字(0 到 9)、del(退格)鍵和 enter 鍵時(shí)獲得回調(diào).從我的測(cè)試中我發(fā)現(xiàn)了這些(至少在我的手機(jī)上)

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) 調(diào)用editText onKeyListener當(dāng)用戶按 del 或 enter 鍵時(shí).當(dāng)用戶按下回車鍵時(shí),onKey一次輸入調(diào)用兩次函數(shù)(我相信這是 ACTION_UP 和ACTION_DOWN).當(dāng)用戶按下 del 時(shí),onKey 被調(diào)用一次(僅適用于ACTION_DOWN) 我不知道為什么.用戶時(shí)永遠(yuǎn)不會(huì)調(diào)用 onKey按下任何數(shù)字(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個(gè)回調(diào)函數(shù)被稱為(在TextChanged之前,onTextChanged, afterTextChanged)每當(dāng)用戶按下任何數(shù)字(0 到9) 鍵.所以我想通過使用TextWatcher 和 onKeyListener 在一起我可以得到我需要的所有回調(diào).

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.

現(xiàn)在我的問題是這些..

Now my questions are these..

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

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) 二、我要格式化數(shù)字用戶輸入新數(shù)字后.說我已經(jīng)有 123 在 editText 和用戶輸入按下 4,我想要我的editText 顯示 1,234.我吃飽了onTextChanged() 和afterTextChanged() 我可以格式化號(hào)碼并將其放回任何這些回調(diào)中的editText.我應(yīng)該使用哪一個(gè)?哪一個(gè)是最佳實(shí)踐?

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) 第三個(gè)是最關(guān)鍵的問題.當(dāng)應(yīng)用程序啟動(dòng)時(shí),我把editText 中的當(dāng)前對(duì)象值.假設(shè)我在 onResume() 上放了 123,當(dāng)用戶輸入一個(gè)數(shù)字(比如 4)我想要它是 1234.但在我的 onTextChanged回調(diào)我得到的是4123.當(dāng)我再按一個(gè)鍵(比如 5) 我是得到 45123.所以對(duì)于用戶輸入editText 光標(biāo)指向末尾文本.但是當(dāng)值由手,editText 光標(biāo)似乎不是更新.我相信我必須做textWatcher 回調(diào)中的某些內(nèi)容,但是我不知道我該怎么辦.

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.

我在下面發(fā)布我的代碼.

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);
    }
}

我知道這是一個(gè)大問題,但我正在處理同樣的問題 2 天.我是 android 新手,仍然無法相信沒有簡(jiǎn)單的方法可以即時(shí)處理 textEdit 數(shù)據(jù)(我在 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}); 

當(dāng)應(yīng)用啟動(dòng)時(shí),我將 1,234 放在 editText 上

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

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

然后當(dāng)用戶點(diǎn)擊editText時(shí),鍵盤彈出,說用戶輸入數(shù)字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

推薦答案

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

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()上,我強(qiáng)制將光標(biāo)放在當(dāng)前EditText文本的末尾,當(dāng)用戶按下任何數(shù)字時(shí)我也做了同樣的事情.希望它對(duì)某人有所幫助..感謝所有試圖提供幫助的人.

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.

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

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

相關(guān)文檔推薦

Cut, copy, paste in android(在android中剪切、復(fù)制、粘貼)
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 位小數(shù)的數(shù)字)
Changing where cursor starts in an expanded EditText(更改光標(biāo)在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 成人午夜免费网站 | 久久精品一级 | 久久蜜桃av一区二区天堂 | 国产精品视频免费播放 | 久久美女网 | 日本不卡一区 | 欧美日韩亚洲系列 | 亚洲视频免费观看 | 欧美激情 一区 | 九色网址 | 丁香五月网久久综合 | 黑人精品欧美一区二区蜜桃 | 国产永久免费 | 国产区一区二区三区 | av一级久久| 国产在线精品一区二区三区 | 国产良家自拍 | 亚洲精视频 | 99精品国产一区二区青青牛奶 | 久久99久久98精品免观看软件 | 午夜影院在线观看 | 精品亚洲永久免费精品 | 久久99国产精品 | 伊人影院99 | 国产精品久久久久久久久久免费看 | 毛片免费看| 国产亚洲精品精品国产亚洲综合 | 国产福利在线 | 国产成人在线一区二区 | 在线免费观看黄色 | 国产精品毛片无码 | 国产精品1区 | 成人小视频在线观看 | 国产精品国产成人国产三级 | 欧美成人免费在线视频 | 91成人小视频 | 成人免费观看男女羞羞视频 | 国产探花在线精品一区二区 | 国产性生活一级片 | 久久99精品久久久久久国产越南 | 午夜精品久久久久久 |