2012-06-02 11 views
11

टाइपिंग करते समय मुझे टेक्स्टटेक्स्ट के अंदर टेक्स्ट को प्रतिस्थापित करने की आवश्यकता है: उदाहरण: यदि उपयोगकर्ता "ए" दबाता है तो इसे एक बफर में संग्रहीत किया जाएगा और इसके बजाय एडिटटेक्स्ट "डी" प्रदर्शित होता है (दिखता है जैसे उसने "डी" दबाया)। अब मैं दबाया चरित्र पढ़ सकते हैं, लेकिन मैं एट में किसी भी चरित्र प्रदर्शित नहीं कर सकता stackoverflow से बचने के लिए:एडिटटेक्स्ट - टाइपिंग करते समय टेक्स्ट बदलें

final EditText et = (EditText) findViewById(R.id.editTexts); 
    final TextView tv = (TextView) findViewById(R.id.textView2); 

    et.addTextChangedListener(new TextWatcher() 
    { 
      public void afterTextChanged(Editable s){} 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       if(s.length() > 0) { 
        tv.setText(s.toString().substring(s.length()-1)); 
        et.setText(""); 
       } 
      } 
    }); 
+0

यह देखने है ................... ....... http://stackoverflow.com/questions/9498155/android-java-update-same-edittext-in-textchanged-event –

+0

s.clear(); आपके टेक्स्ट को सेट करने के बाद – Pradeep

+1

एक ही प्रश्न दो बार http://stackoverflow.com/questions/10862343/access-edittext-from-textwatcher#10862398 – Ishu

उत्तर

22

आप के रूप में इसे बदल सकते हैं देखें आवश्यक ::

public class SampleActivity extends Activity { 
    TextWatcher tt = null; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     final EditText et = (EditText) findViewById(R.id.editText1); 
     final TextView tv = (TextView) findViewById(R.id.textView1); 
     tt = new TextWatcher() { 
      public void afterTextChanged(Editable s){ 
       et.setSelection(s.length()); 
      } 
      public void beforeTextChanged(CharSequence s,int start,int count, int after){} 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       et.removeTextChangedListener(tt); 
       et.setText(et.getText().toString().replace("A", "C")); 
       et.addTextChangedListener(tt); 
      } 
     }; 
     et.addTextChangedListener(tt); 
    } 
} 
+0

और "ए" के साथ "ए" को प्रतिस्थापित करता है लेकिन इसे उलट दिया जाता है, यह –

+1

फ़ील्ड की शुरुआत में वर्ण जोड़ता है, शायद इसे ET.setSelection (s.length()) की आवश्यकता हो; कहीं !! –

7

पाठ को अंतःक्रियात्मक रूप से बदलने के लिए, आपको TextWatcher पंजीकृत करना होगा। लेकिन दर्शक के अंदर पाठ को बदलने की कोशिश करने से वॉचर को और कॉल मिलती है। मेरे हैक अस्थायी रूप से द्रष्टा दूर करने के लिए जब मैं पाठ को बदलना चाहते हैं, और यह पुन: पंजीकृत सही होने के बाद

mEditText.addTextChangedListener(new TextWatcher() { 
     @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } 
     @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } 
     @Override public void afterTextChanged(Editable editable) { 
      mEditText.removeTextChangedListener(this); 
      mEditText.setText(//TODO change whatever you like here); 
      mEditText.setSelection(editable.length()); //moves the pointer to end 
      mEditText.addTextChangedListener(this); 
     } 
संबंधित मुद्दे