Button不是放在RelativeLayout中的
xml:
<?xml version="1.0" encoding="utf-8"?> <Button xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/myButton" android:text="My Button" />
package com.example.androidtest; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button myButton = (Button)findViewById(R.id.myButton); myButton.setOnClickListener(clickListener); } View.OnClickListener clickListener = new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } }; }原文链接:https://www.f2er.com/xml/298926.html