我从Kotlin开始,如果我在Activity Class上实现它,似乎onClick不会触发
class MainActivity : AppCompatActivity(), View.OnClickListener{
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tvTitle.text = "Hi There"
// tvTitle.setOnClickListener { this } // NOT WORKING??
tvTitle.setOnClickListener { doSomething() }
}
override fun onClick(v: View) {
Log.d("click", "Hello")
}
fun doSomething(){
Log.d("do", "Something")
}
}
我使用的是带有1.1.51 kotlin_version的Android Studio 3.0,提前谢谢
答案 0 :(得分:7)
试试这个
class MainActivity : AppCompatActivity(), View.OnClickListener{
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tvTitle.text = "Hi There"
tvTitle.setOnClickListener(this)
// tvTitle.setOnClickListener { doSomething() }
}
override fun onClick(v: View) {
Log.d("click", "Hello")
}
fun doSomething(){
Log.d("do", "Something")
}
}
答案 1 :(得分:2)
这是我的kotlin代码:
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.LinearLayout
import android.widget.Toast
class Activity_T : AppCompatActivity(), View.OnClickListener {
private var linear_exit: LinearLayout? = null
private var linear_history: LinearLayout? = null
private var linear_recipe: LinearLayout? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initView()
setOnClicks()
}
private fun setOnClicks() {
linear_exit!!.setOnClickListener(this)
linear_history!!.setOnClickListener(this)
linear_recipe!!.setOnClickListener(this)
}
private fun initView() {
linear_exit = findViewById(R.id.linear_exit)
linear_history = findViewById(R.id.linear_history)
linear_recipe = findViewById(R.id.linear_recipe)
}
override fun onClick(v: View) {
val item_id = v.id
when (item_id) {
R.id.linear_recipe -> Toast.makeText(this, "recipe", Toast.LENGTH_SHORT).show()
R.id.linear_history -> Toast.makeText(this, "history", Toast.LENGTH_SHORT).show()
R.id.linear_exit -> Toast.makeText(this, "exit", Toast.LENGTH_SHORT).show()
}
}
}
这是我的具有onclicks的xml布局的一部分:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linear_exit"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/border_green"
android:gravity="center">
<net.kibotu.heartrateometer.utils.MyTextView
android:textSize="13sp"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/exit" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear_history"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/border_green"
android:gravity="center">
<net.kibotu.heartrateometer.utils.MyTextView
android:textSize="13sp"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/history" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear_recipe"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@color/border_green"
android:gravity="center">
<net.kibotu.heartrateometer.utils.MyTextView
android:textSize="13sp"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/recipe" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
答案 2 :(得分:0)
首先尝试 println(&#34; Something&#34;)。
答案 3 :(得分:0)
您可以尝试这种方式。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val tvTitle = findViewById<TextView>(R.id.tvTitle)
tvTitle.setText("Hello Kotlin")
tvTitle.setOnClickListener(object : View.OnClickListener{
override fun onClick(v: View?) {
Toast.makeText(this@MainActivity,"You Clicked on " + tvTitle.text.toString(),Toast.LENGTH_SHORT).show()
}
})
}