Button的基本属性:
<Button
android:id="@+id/btn_show"
android:text="@string/btn_label"
android:textSize="28sp"
android:textColor="@color/colorAccent"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnClick"/>
监听按钮点击事件:
步骤1.获取控件对象
步骤2.注册监听器
步骤3.编写响应代码
示例:
package com.example.cerizoapp;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {// implements View.OnClickListener {
// 声明控件对象
// Button btn_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setContentView(R.layout.main); // 加载布局文件
// btn_show = findViewById(R.id.btn_show); // 拿到对象
// 注册监听器
// btn_show.setOnClickListener(new View.OnClickListener() { // 匿名内部类
// @Override
// public void onClick(View v) {
// // 响应
// String text = "您点击了按钮";
// Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();
// Log.i("lhj",text);
// }
// });
// btn_show.setOnClickListener(this);
}
// @Override
// public void onClick(View v) { // 实现接口
// 响应
// String text = "您点击了按钮";
// Toast.makeText(this,text,Toast.LENGTH_SHORT).show();
// Log.i("lhj",text);
// }
public void btnClick(View view) { // 自定义的方法
// 响应
String text = "您点击了按钮!";
Toast.makeText(this,text,Toast.LENGTH_SHORT).show();
Log.i("lhj",text);
}
}
三种注册监听方式比较:
1.匿名内部类:
需要获取控件对象,使用变量不方便;适用于单个事件
2.实现接口:
需要获取控件对象,使用变量方便;适用于多个事件
3.设置onClick属性:
无需获取控件对象,使用变量方便;不便维护
示例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/ed_search"
android:hint="@string/search"
android:inputType="text"
android:layout_width="180dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_show"
android:text="@string/btn_label"
android:textSize="28sp"
android:textColor="@color/colorAccent"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!-- android:onClick="btnClick"-->
<Button
android:id="@+id/btn_quit"
android:text="@string/btn_quit"
android:textSize="28sp"
android:textColor="@color/colorAccent"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/iv_logo"
android:src="@drawable/ic_launcher_background"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
package com.example.cerizoapp;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// 声明控件对象
Button btn_show, btn_quit;
EditText ed_search;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setContentView(R.layout.main); // 加载布局文件
btn_show = findViewById(R.id.btn_show); // 拿到对象
btn_quit = findViewById(R.id.btn_quit);
ed_search = findViewById(R.id.ed_search);
// 注册监听器
// btn_show.setOnClickListener(new View.OnClickListener() { // 匿名内部类
// @Override
// public void onClick(View v) {
// // 响应
// String text = "您点击了按钮";
// Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();
// Log.i("lhj",text);
// }
// });
btn_show.setOnClickListener(this);
btn_quit.setOnClickListener(this);
}
@Override
public void onClick(View v) { // 实现接口
// 响应
String text = ed_search.getText().toString();
switch (v.getId()){
case R.id.btn_show:
text += "按钮1";
break;
case R.id.btn_quit:
text += "按钮2";
break;
}
// String text = "您点击了按钮";
Toast.makeText(this,text,Toast.LENGTH_SHORT).show();
Log.i("lhj",text);
}
// public void btnClick(View view) { // 自定义的方法
// // 响应
// String text = "您点击了按钮!";
// Toast.makeText(this,text,Toast.LENGTH_SHORT).show();
// Log.i("lhj",text);
// }
}
总结:
监听按钮点击事件的三种方法;
匿名内部类中使用this对象需要添加类名;
EditText可以通过hint属性设置提示信息;
ImageView的src和background属性的区别;
LogCat日志的输出总共分5个级别,可以通过级别和标签进行过滤;
--_erizo