1.常见事件监听接口及处理方法
事件 | 接口 | 处理方法 | 说明 |
---|---|---|---|
单击事件 | OnClickListener | onClick() | 单击组件时触发 |
单击事件 | OnLongClickListener | onLongClick() | 长安组件时触发 |
键盘事件 | OnkeyListener | onKey() | 处理键盘事件 |
焦点事件 | OnFocusChangeListener | onFocusChange | 当焦点发生改变时触发 |
触摸事件 | OnTouchListener | onTouch | 当屏幕上触摸组件时触发 |
编辑活动时间 | OnEditorActionListener | onEditorAction | 当编辑时触发 |
2.View类的常见事件监听器注册方法
方法 | 说明 |
---|---|
setOnClickListener | 注册单击事件监听器 |
setOnLongClickListener | 注册长按事件监听器 |
setOnkeyListener | 注册键盘事件监听器 |
setOnFocusChangeListener | 注册焦点事件监听器 |
setOnTouchListener | 注册触摸事件监听器 |
setOnEditorActionListener | 注册编辑活动时间监听器 |
案例:
Layout布局
<?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">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试文字"
android:layout_margin="20dp"
android:layout_gravity="center_horizontal"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="颜色:"/>
<Button
android:id="@+id/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红色"/>
<Button
android:id="@+id/green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绿色"/>
<Button
android:id="@+id/blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蓝色"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="字体"/>
<Button
android:id="@+id/bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="粗体"/>
<Button
android:id="@+id/italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="斜体" />
<Button
android:id="@+id/normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="默认"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="字体大小:"/>
<Button
android:id="@+id/bigger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增大"/>
<Button
android:id="@+id/smaller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减小"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内容:"/>
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
Mainactivity:
package com.example.a4_com;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView text;
private Button red,green,blue,bigger,smaller,bold,italic,naomal;
private EditText edit;
private int flot=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//颜色事件获取组件
text=(TextView) findViewById(R.id.text);
red=(Button) findViewById(R.id.red);
green=(Button)findViewById(R.id.green);
blue=(Button) findViewById(R.id.blue);
//实现事件监听器
InterColour interColour=new InterColour();
//注册事件监听器
red.setOnClickListener(interColour);
green.setOnClickListener(interColour);
blue.setOnClickListener(interColour);
//文字大小事件
bigger=(Button) findViewById(R.id.bigger);
smaller=(Button) findViewById(R.id.smaller);
OuterTypeface outerTypeface=new OuterTypeface();
bigger.setOnClickListener(outerTypeface);
smaller.setOnClickListener(outerTypeface);
//字体样式事件
bold=(Button) findViewById(R.id.bold);
italic=(Button) findViewById(R.id.italic);
naomal=(Button) findViewById(R.id.normal);
bold.setOnClickListener(this);
italic.setOnClickListener(this);
naomal.setOnClickListener(this);
//编辑框文件
edit=(EditText) findViewById(R.id.edit);
//4.匿名内部类
edit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
text.setText(edit.getText().toString().trim());
return true;
}
});
}
//1.定义内部类
public class InterColour implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.red:
text.setTextColor(Color.RED);
break;
case R.id.green:
text.setTextColor(Color.GREEN);
break;
case R.id.blue:
text.setTextColor(Color.BLUE);
break;
default:
break;
}
}
}
//3.使用类自身继承
//flot=0 默认 flot=1加粗 flot=2 倾斜 flot=3加粗又倾斜
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bold:
if(flot==2||flot==3){
text.setTypeface(Typeface.MONOSPACE,Typeface.BOLD_ITALIC);
flot=3;
} else{
text.setTypeface(Typeface.MONOSPACE,Typeface.BOLD);
flot=1;
}break;
case R.id.italic:
if (flot==1||flot==3){
text.setTypeface(Typeface.MONOSPACE,Typeface.BOLD_ITALIC);
flot=3;
}else {
text.setTypeface(Typeface.MONOSPACE,Typeface.ITALIC);
flot=2;
}break;
case R.id.normal:
text.setTypeface(Typeface.MONOSPACE,Typeface.NORMAL);
flot=0;
break;
default:
break;
}
}
}
2.外部类:
package com.example.a4_com;
import android.view.View;
import android.widget.TextView;
public class OuterTypeface implements View.OnClickListener {
private TextView text;
private float size=10;
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bigger:
size+=4;
break;
case R.id.smaller:
size-=4;
break;
default:
break;
}
if (size>=72){
size=72;
if (size<=4) {
size = 4;
}
}
}
}