2012-05-16 17 views
6

लपेट नहीं पाएगा, मुझे पता है कि स्टैक ओवरफ्लो में बहुत से संबंधित प्रश्न हैं, लेकिन मैंने उनमें से किसी भी में लिखे गए सब कुछ करने की कोशिश की और अब तक मैं यह काम नहीं कर सका।एडिटटेक्स्ट अपना टेक्स्ट

मेरी समस्या सरल है। मेरे पास एंड्रॉइड एप्लिकेशन है, और मेरे पास एक बॉक्स है जहां उपयोगकर्ता टेक्स्ट इनपुट करेगा। हालांकि, मैं नहीं चाहता कि यह टेक्स्ट मल्टीलाइन हो (इसमें इसमें "\ n" नहीं होना चाहिए), लेकिन अगर मैं व्यूपोर्ट से अधिक लाइन का आकार बड़ा करता हूं तो मैं इसे लपेटना चाहता हूं। तो मूल रूप से, जो मैं चाहता हूं वह वही व्यवहार है जो जीमेल विषय फ़ील्ड पर टेक्स्टबॉक्स है।

यह मेरे पूरे दृश्य है:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

<TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/question" 
     android:textAppearance="@style/titleFont"/> 

<EditText 
    android:id="@+id/edit_question_value" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.3" 
    android:gravity="top" 
    android:inputType="textImeMultiLine|textEmailSubject" 
    android:maxLength="70" 
     > 

</EditText> 

<TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/answer" 
     android:textAppearance="@style/titleFont"/> 

<EditText 
     android:id="@+id/edit_question_answer" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:gravity="top" 
     android:layout_weight="0.7" 
     android:inputType="textMultiLine"/> 

<LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal"> 

    <Button 
      android:id="@+id/save_question" 
      android:layout_width="120dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="0.5" 
      android:text="@string/save"/> 

    <Button 
      android:id="@+id/cancel_edit_question" 
      android:layout_width="120dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:text="@string/cancel"/> 
</LinearLayout> 

पाठ मैं इस व्यवहार करना चाहते हैं theone साथ आईडी + id/edit_question_value @ के बराबर है। अब तक, मैं इसे सचमुच बहुमुखी बनाने में सक्षम हूं (इसलिए यदि उपयोगकर्ता चाहता है तो उपयोगकर्ता प्रवेश कर सकता है) या सिंगल-लाइन और टेक्स्ट लपेट नहीं जाएगा। शायद पाठ के आस-पास के कुछ तत्वों में कुछ हस्तक्षेप हो रहा है, और यही कारण है कि मैंने पूरा दृश्य शामिल किया।

क्या कोई जानता है कि क्या हो रहा है?

उत्तर

8

यह ऐसा कुछ है जिसने बहुत से लोगों को परेशान किया है और मुझे इसकी आवश्यकता होने पर एक जवाब मिल सकता है। शुद्ध एक्सएमएल में, वह है। यदि हम प्रोग्रामिंग का उपयोग कर सकते हैं और न केवल मार्कअप, तो हम इसे हैक के साथ कर सकते हैं यदि आप कर सकते हैं। असल में हम एक बहुउद्देश्यीय संपादन टेक्स्ट घोषित कर रहे हैं और फिर नई लाइनों से बचने के लिए रिटर्न कुंजी को पकड़ रहे हैं।

पहले एक्सएमएल में घोषणा:

<EditText 
    android:id="@+id/noReturnEditText" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:imeOptions="actionDone" 
    android:inputType="textMultiLine|textCapSentences" 
    android:text="lorem ipsum \n dolor site amet helping stackoverflow" /> 

हाँ, मुझे पता है तुम क्या सोच रहे हैं "लेकिन हे, जुआन यह एक multiliner EditText है और मैं एक एक लाइनर विषय पर की तरह चाहते हैं जीमेल का क्षेत्र "। मुझे पता है, मुझे पता है, लेकिन हम अभी इस बात का ख्याल रखने जा रहे हैं, मल्टीलाइन भाग के व्यवहार को हटाकर हम नहीं चाहते हैं, जबकि टेक्स्ट रैपिंग को रखते हुए हम चाहते हैं।

अब कोड के बाकी:

EditText noReturnEditText = (EditText) findViewById(R.id.noReturnEditText); 
     noReturnEditText.setOnKeyListener(new OnKeyListener(){ 
      @Override 
      public boolean onKey(View v, int keyCode, KeyEvent event) { 
       if (keyCode == KeyEvent.KEYCODE_ENTER) return true; 
       return false; 
      } 
     }); 

आप शायद काफी आसानी से कोड को पढ़ सकता है, लेकिन इस मामले में कोई एक व्याख्या है, हम तय कर रहे हैं एक EditText को OnKeyListener हम होना चाहते हैं "की जरूरत है ऑनलाइन-मल्टीलिनेलिक "और यदि उपयोगकर्ता रिटर्न कुंजी (KEYCODE_ENTER) दबाता है तो हम ईवेंट को true लौटकर प्रचार नहीं करना चाहते हैं, जिसका अर्थ है कि हमने ईवेंट को संभाला है।

परिणाम:

enter image description here

विचार

वापस कुंजी दबाने एक नई लाइन में प्रवेश करने के लिए एक ही रास्ता नहीं है। उपयोगकर्ता कुछ पाठ कॉपी/पेस्ट कर सकता है और एक सिंगल-लाइन एडिटटेक्स्ट पर एक बहु-पंक्ति प्रविष्टि के साथ समाप्त हो सकता है। परवाह नहीं!हम मूल्य पट्टी कर सकते हैं इससे पहले कि हम यह स्टोर या

myData = rawText.replace(System.getProperty("line.separator"), " ");

की तरह कुछ के साथ उपयोग करें क्या होगा यदि मैं EditText विचारों के बहुत सारे है?

ठीक है, आपके मूल रूप से दो विकल्प हैं। इसे एक करके करें और अपनी आंखें खींचें या ऐसा कुछ करें, इसकी प्रत्येक दृश्य के लिए दृश्य और लूपिंग सूचीबद्ध करें।

//Declare the reusable listener 
    OnKeyListener myListener = new OnKeyListener(){ 
     @Override 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      if (keyCode == KeyEvent.KEYCODE_ENTER) return true; 
      return false; 
     } 
    } 
//Loop through the views 
    int viewIds[] = {R.id.edittext1,R.id.edittext2,R.id.edittext3,R.id.edittext4}; 
    for(int id : viewIds) ((EditText)findViewById).setOnKeyListener(myListener); 
संबंधित मुद्दे