web-dev-qa-db-ja.com

アクティビティがKotlinで記述されている場合、ボタンのonClick属性はありません

このチュートリアルに従ってください: Android-Start Another ActivityMainActivity.JavaボタンOnClick属性にsendMessage()メソッドを設定した場合。

ただし、MainActivity.ktボタンOnClick属性に表示するものがない場合は、noneのみを作成します。

これはAndroid St​​udio 3のバグですか、それともKotlinの何かを逃しましたか?

Java 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
    }
}

OnClick attribute

XMLレイアウト(Javaと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>
23
Vahid

デザイナーはまだKotlinをサポートしていないようです。解決策は次のとおりです。

XML(非推奨)

次の行をButtonタグに追加します。これはまさにデザイナーが行うことです。

Android:onClick="sendMessage"

古いファッション

何も追加する必要はありません。

val button = findViewById<Button>(R.id.Button)
button.setOnClickListener {

}

kotlin-Android-extensions(推奨)

Build_gradleにapply plugin: "kotlin-Android-extensions"を追加します

// button is the Button id
button.setOnClickListener {

}
29
Joshua

コードは次のようになります。

button.setOnClickListener(){
            Toast.makeText(this@MainActivity, "Its toast!", Toast.LENGTH_SHORT).show();
        }

ここでインポートされます

import kotlinx.Android.synthetic.main. activity_main.*

ここで、「ボタン」は.xmlファイル内のそのボタンのIDです。ここでは、JavaクラスにButtonオブジェクトを作成する必要がないという利点があります。

3

SendMessageクラスを次のように定義したら:

/** Called when the user taps the Send button  */
fun sendMessage(view: View) {
    setContentView(R.layout.activity_second)
    // Do something in response to button
}

また、2番目のアクティビティを次のように定義しました。

class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
    }
}

OnClick関数にSendMessageを追加しました: enter image description here

そして、それはうまくいきました。

2
Alex

MainActivity.ktファイルで考え出したソリューションを次に示します。

override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val button = findViewById<Button>(R.id.button)
    button.setOnClickListener {
        sendMessage()
    }
}

/** Called when the user taps the Send button  */
private fun sendMessage() {
    val editText = findViewById<EditText>(R.id.editText)
    val message = editText.text.toString()
    val intent = Intent(this, DisplayMessageActivity::class.Java).apply 
    {
        putExtra(EXTRA_MESSAGE, message)
    }
    startActivity(intent)
}
1
John

これはXML自体の中で簡単に定義できます。ただし、Android:onClick属性の使用にはまだ少し費用がかかります。

代わりに、 Kotlin Android Extensions および合成プロパティの使用を検討できます。

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    button.setOnClickListener {
        // Do something in response to button
    }
}
1
tynn