我是kotlin的新手。在这里,我正在为我的应用程序实现登录屏幕,并使用kotlin如下所示使用xml文件实现类,但是当我点击按钮或textview onclick时没有响应。请帮我解决这个问题。
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.*
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
class login : AppCompatActivity(), View.OnClickListener {
@BindView(R.id.title) var title: TextView? = null
@BindView (R.id.email) var email : TextView? = null
@BindView (R.id.password) var pass : TextView? = null
@BindView (R.id.tv_fp) var fp : TextView? = null
@BindView (R.id.tv_register) var register : TextView? = null
@BindView (R.id.btn_login ) var login : Button? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
ButterKnife.bind(this)
// change to register!!.setOnClickListener(this) to make sure your view is injected
register?.setOnClickListener(this)
login?.setOnClickListener(this)
}
// v--- change the parameter type View? to View, it is never be null
override fun onClick(p0: View?) {
// v--- remove it
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
// v--- remove the redundant safe-call ?.
if(p0?.id == R.id.tv_register){
Log.e("login", "i")
var intent = Intent(this@login, signin:: class.java)
startActivity(intent)
}
if(p0?.id == R.id.btn_login){
Toast.makeText(this, "Login Button clicked", Toast.LENGTH_SHORT)
}
}
}
和这个类的布局文件如下,我使用constrainedLayout
<?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"
android:layout_margin="20dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/email"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Email id"
android:textCursorDrawable="@drawable/cursor_color"
android:backgroundTint="@color/skyBlue"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="100dp"
app:layout_constraintHorizontal_bias="0.0"
android:inputType="textEmailAddress"/>
<android.support.design.widget.TextInputEditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Password"
android:textCursorDrawable="@drawable/cursor_color"
android:id="@+id/password"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/email"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
android:inputType="textPassword"/>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="36dp"
android:background="@color/yellow"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="@+id/password"
android:layout_marginStart="4dp" />
<TextView
android:id="@+id/tv_fp"
android:layout_width="171dp"
android:layout_height="22dp"
android:text="FORGOT PASSWORD?"
android:background="?attr/selectableItemBackground"
tools:layout_editor_absoluteY="301dp"
android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/tv_register"
android:layout_marginTop="6dp"
android:layout_marginBottom="7dp"
app:layout_constraintBottom_toTopOf="@+id/btn_login" />
<TextView
android:id="@+id/tv_register"
android:layout_width="156dp"
android:layout_height="18dp"
android:layout_marginStart="8dp"
android:text="Register"
android:background="?attr/selectableItemBackground"
android:gravity="right"
app:layout_constraintStart_toEndOf="@+id/tv_fp"
app:layout_constraintTop_toTopOf="@+id/button"
android:layout_marginTop="58dp"
app:layout_constraintBottom_toBottomOf="@+id/btn_login"
/>
</android.support.constraint.ConstraintLayout>