2016-05-13 18 views
9

तो मैं इस जावा कोड है:Kotlin साथ setOnEditorActionListener कैसे

editText.setOnEditorActionListener() { v, actionId, event -> 
     if(actionId == EditorInfo.IME_ACTION_DONE){ 
      doSomething() 
     } else { 
     } 
} 

: मैं इस (जो मैं भी यकीन है कि यह सही तरीका है नहीं कर रहा हूँ) प्राप्त करने के लिए प्रबंधित किया है

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
     if (actionId == EditorInfo.IME_ACTION_DONE) { 
      doSomething(); 
      return true; 
     } 
     return false; 
    } 
}); 

लेकिन मुझे एक त्रुटि मिली है Error:(26, 8) Type mismatch: inferred type is kotlin.Unit but kotlin.Boolean was expected

तो कोटलिन में ऐसे ईवेंट हैंडलर को कैसे लिखा जाता है?

+0

हां, उसी परिणाम – Pier

उत्तर

19

onEditorActionBoolean देता है जबकि आपका कोटलिन लैम्ब्डा Unit देता है। यह अर्थात में बदलें:

editText.setOnEditorActionListener() { v, actionId, event -> 
     if(actionId == EditorInfo.IME_ACTION_DONE){ 
      doSomething() 
      true 
     } else { 
      false 
     } 
} 

The documentation लैम्ब्डा भाव और अनाम कार्यों पर एक अच्छा पढ़ा है।

+1

के बजाय सही/गलत का उपयोग करना होगा, कृपया मुझे बताएं कि {{v, actionId, event -> 'यह विषय कौन सा है? – sushildlh

+0

@ सुशील्डह वहां आप जाते हैं। – miensol

+0

thanx ........... क्या कोई पुस्तकालय हमें एंड्रॉइड में लागू करना है? – sushildlh

0

Kotlin साथ बहुत अच्छा होगा जब बजाय यदि किसी और

मेरे लिए का उपयोग कर के कीवर्ड, निम्नलिखित कोड अधिक सुंदर है:

editText.setOnEditorActionListener() { v, actionId, event -> 
    when(actionId)){ 
     EditorInfo.IME_ACTION_DONE -> doSomething(); true 
     else -> false 
    } 
} 

पी/एस: @Pier का कोड लैम्ब्डा के दाहिने तरफ एक अभिव्यक्ति की वजह से काम नहीं कर रहा है। इसलिए, हमें सही/झूठी