按钮Button及按钮处理事件
按钮Button用于处理人机交互的事件,在一般应用程序中常常会用到。由于按钮Button是文本标签TextView的子类,按钮Button继承了文本标签TextView所有的方法和属性。其继承关系如图所示。
按钮Button在程序设计中最常用的方式是实现OnClickListener监听接口,当单击按钮时,通过OnClickListener监听接口触发onClick()事件,实现用户需要的功能。 OnClickListener接口有一个onClick()方法,在按钮Button实现OnClickListener接口时,一定要重写这个方法。 按钮Button调用OnClickListener接口对象的方法如下:
按钮对象.setOnClickListener(OnClickListener对象);
实战测试:
编写程序,当点击按钮命令后,页面标题及文本组件的文字内容发生变化。
相关代码文件:
(1)资源文件strings.xml
(2)布局文件activity_main.xml;
(3)控制文件MainActivity.java;
(1)资源文件strings.xml
<resources>
<string name="app_name">Liangxiu</string>
<string name="sentence">Hello,\n Android!</string>
<string name="sentence_harp">Here is\n Liang Xiu!</string>
<string name="button_text">点击我</string>
</resources>
(2)布局文件:activity_main.xml
在布局文件中添加一个按钮,其id设置为name_button01。
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:orientation="vertical"><!--布局内组件摆放方向:垂直-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sentence"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:textSize="30sp"
android:textColor="#FFFF0000"
android:background="@color/black"
android:id="@+id/name_sentence"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:textSize="20sp"
android:id="@+id/name_button01"/>
</LinearLayout>
(3)控制文件: MainActivity.java
在控制文件中,创建一个按钮对象,并且把按钮对象与布局层按键关联起来:findViewById()
调用按键的监听方法,创建一个内部类click:
按键.setOnClickListener(new click)
内部类click要继承接口View.OnClickListener :
重写click中的onClick()事件,实现当点击按钮时,触发onClick()事件。
package com.example.cst22078;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView text;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=findViewById(R.id.name_sentence);
button=findViewById(R.id.name_button01);
button.setOnClickListener(new click());
}
class click implements View.OnClickListener{
@Override
public void onClick(View view){
MainActivity.this.setTitle("改变文本");
text.setText(R.string.sentence_harp);
text.setTextSize(30);
text.setTextColor(Color.BLACK);
text.setBackgroundColor(0xffff0000);
}
}
}
监听函数的另一种调用:
public class MainActivity extends AppCompatActivity {
TextView text;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=findViewById(R.id.name_sentence);
button=findViewById(R.id.name_button01);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
MainActivity.this.setTitle("改变文本");
text.setText(R.string.sentence_harp);
text.setTextSize(30);
text.setTextColor(Color.BLACK);
text.setBackgroundColor(0xffff0000);
}
});
}
}
最终效果:
点击前:
点击后:
相关链接:
初级Android开发学习笔记(二)图形用户界面设计-TextView
标签:OnClickListener,text,Button,按钮,import,Android,button,图形用户界面 From: https://blog.csdn.net/2401_83587546/article/details/136747352