Copying text from one EditText box to another in the same Activity on the fly, character-by-character

删除回忆录丶 提交于 2020-01-15 05:28:30

问题


I am building an application to convert Latitude and Longitudes between various formats. My layout is complete but my current challenge is this:

For the Degree values, as the user enters a value in EditText1, I want it to replicate in EditText2 as they are typing, real-time, character for character. I have beat myself up trying OnTouchListener, onKeyboardActionListener, etc.

Later, I will perform calculations on the Minutes, Seconds, and decimal portions as they are typed in. Since the Degree field does not require calculations I am just trying to replicate the users value across multiple EditText boxes for now.

SUMMARY: Capture the characters in EditText1 as they are typed Place EditText1 captured characters into EditText2 on the fly.

Any help would be greatly appreciated.

Mike Murphy


回答1:


I haven't tested, but I found addTextChangedListener():

EditText firstEditText = (EditText)findViewById(R.id.firstEditText);
firstEditText.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s){
        String c = s.toString(); // read Content
        ((EditText)findViewById(R.id.secondEditText)).setText(c); // copy to #2
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after){ }
    public void onTextChanged(CharSequence s, int start, int before, int count){ }
});

http://groups.google.com/group/android-developers/browse_thread/thread/eba1a2ea7d3a2828?fwc=1



来源:https://stackoverflow.com/questions/4611388/copying-text-from-one-edittext-box-to-another-in-the-same-activity-on-the-fly-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!