Tôi đang làm các mô hình onClick đơn giản nhất và không thể có phương thức onClick để kích hoạt. Tôi biết nó là một cái gì đó đơn giản, và tôi mới đến với Android. Bất kỳ trợ giúp được đánh giá cao.phương thức onClick không được gọi trên Android
package com.bordeloniphone.timeentry;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class TimeEntryActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
Button okButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
okButton = (Button) findViewById(R.id.btnOK);
okButton.setText(":)");
okButton.setOnClickListener(this);
//setContentView(okButton);
}
public void onClick(View v) {
Log.d("TEST", "TEST");
Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
}
}
Đây là main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnOK"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>
Tôi sao chép/dán mã chính xác của bạn vào một dự án mới và nó hoạt động. Tôi nhận được bánh mì nướng và mục nhập LogCat. Điều gì khiến bạn nghĩ rằng nó không hoạt động? – kabuko
Mã của bạn hoạt động tốt cho tôi..Kiểm tra sau khi làm sạch và xây dựng dự án của bạn .. – deepa
Có, mã này hoạt động cho tôi. Có lẽ bạn có thể thử: Toast.makeText (TimeEntryActivity.this, "TEST", Toast.LENGTH_SHORT) .show(); – R4j