में लिखा गतिविधि इस ट्यूटोरियल का पालन करें कोई भी है।बटन onclick विशेषता अगर Kotlin
लेकिन अगर मैंने MainActivity.kt
बटन OnClick
विशेषता को दिखाने के लिए कुछ भी नहीं है, तो केवल none
।
क्या यह एंड्रॉइड स्टूडियो 3 बग है या मुझे कोटलिन के लिए कुछ याद आया?
जावा MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
}
Kotlin MainActivity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
/** Called when the user taps the Send button */
fun sendMessage(view: View) {
// Do something in response to button
}
}
एक्सएमएल लेआउट (जावा और Kotlin परियोजना एक ही हैं)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ir.bigbang.vahid.myapplication.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="81dp" />
</android.support.constraint.ConstraintLayout>
क्या आप अपनी लेआउट फ़ाइल एक्सएमएल में पोस्ट कर सकते हैं? लेकिन मैं आपको लेआउट फ़ाइल पर 'onClick' सेट करने की अनुशंसा नहीं करता क्योंकि आप एक व्यू फ़ाइल पर प्रोग्रामिंग तर्क डाल रहे हैं। कोटलिन-एंड्रॉइड-एक्सटेंशन के साथ, आप आईडी द्वारा दृश्य तक पहुंच सकते हैं, लेआउट में इसे सेट करने की कोई बात नहीं है। – Joshua
@ जोशुआ, लेआउट जोड़ा गया। – Vahid