Button控件的实现形式主要分三种:
1.button控件匿名类实现OnClickListener监听 重写onClick方法
2.布局文件定义android:onClick="方法名" 不需要加括号 再定义public void 方法名(View view)
3.Activity类实现OnClickListener监听 重写onClick方法 再根据id定位控件
优先度(由高到底执行顺序):1>3>2
界面:
样式:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mainview" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.liang.buttonndemo.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button1" android:id="@+id/button1" android:layout_alignParentTop="true" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button2" android:id="@+id/button2" android:layout_below="@+id/button1" android:layout_alignParentStart="true" android:onClick="xmlClick" android:layout_marginTop="25dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button3" android:id="@+id/button3" android:layout_below="@+id/button2" android:layout_alignParentStart="true" android:layout_marginTop="26dp" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton" android:src="@drawable/androidbtn" android:layout_below="@+id/button3" android:layout_alignParentStart="true" android:layout_marginTop="29dp" /> </RelativeLayout>
代码:
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.Toast; //按钮的三种方法 优先度 方法1>方法三>方法二 监听方法>实现方法>xml定义onclick方法 public class MainActivity extends AppCompatActivity implements View.OnClickListener { private View view; private Button button1; private Button button2; private Button button3; private ImageButton imageButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); view = this.findViewById(R.id.mainview); button1 = (Button) this.findViewById(R.id.button1); button2 = (Button) this.findViewById(R.id.button2); button3 = (Button) this.findViewById(R.id.button3); imageButton = (ImageButton) this.findViewById(R.id.imageButton); button1.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); imageButton.setOnClickListener(this); //只有view有onclicklistener事件 view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "view click", 1).show(); } }); //方法1 设置监听 button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "button1 click", 1).show(); } }); //长按监听 button1.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { Toast.makeText(MainActivity.this, "button1 long click", 1).show(); return false; } }); imageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "imagebutton click", 1).show(); } }); } //方法二 响应按钮点击 //xml里面设置android:onclick里面方法只需要名字 不需要括号 //1.必须public 2.必须void 3.形参必须只有一个 而且为View对象 public void xmlClick(View view) { Toast.makeText(MainActivity.this, "button2 click", 1).show(); } //方法三 //实现OnClickListener接口 重写onclick方法 控件设置监听方法setOnClickListener(this) @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: Toast.makeText(MainActivity.this, "method 3 button1 click", 1).show(); break; case R.id.button2: Toast.makeText(MainActivity.this, "method 3 button2 click", 1).show(); break; case R.id.button3: Toast.makeText(MainActivity.this, "method 3 button3 click", 1).show(); break; case R.id.imageButton: Toast.makeText(MainActivity.this, "method 3 imagebutton click", 1).show(); break; default: Toast.makeText(MainActivity.this, "others click", 1).show(); break; } } }
时间: 2024-10-14 01:56:43