这是Mainactivi:
package btn;
import com.example.btn.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Btnactivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO 自动生成的方法存根
Toast.makeText(Btnactivity.this,
"按钮单击",
Toast.LENGTH_SHORT).show();
}
});
这是xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:name="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button1"
/>
<Button
android:name="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button2"
/>
</LinearLayout>
这是报错:
package btn;
import com.example.btn.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Btnactivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO 自动生成的方法存根
Toast.makeText(Btnactivity.this,
"按钮单击",
Toast.LENGTH_SHORT).show();
}
});
这是xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:name="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button1"
/>
<Button
android:name="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button2"
/>
</LinearLayout>
这是报错: