Android常用控件

  • View类

View 类是所有 Android 控件和容器的父类,常见属性如图所示。

 (1)TextView控件

TextView 控件继承自 View 类,用于在界面上显示一段文本信息。除了拥有View类的属性,还发展了些自己的属性,常用的如下图所示。

 

(2)EditText控件

EditText 控件允许用户在控件里输入和编辑内容,并可以在程序中对这些内容进行处理。其继承自 TextView 控件,具有 TextView 的所有属性的同时,还有自己的属性。

 

  • Button控件

Button 是 Android 开发按钮控件,也继承于 TextView 控件。

 

在平时操控软件时常用的便是滑动、点击按键。根据用户不同的操作执行不同的指令,这就是监听。在Andriod中,对Button(也就是按钮)设置监听常用的有四种方式:

(1)通过设置按钮的 onClick 属性来监听按钮的单击操作。

在 activity 中建立一个监听方法,然后通过按钮的 onClick 属性与之关联,具体操作如下:

①在 MainActivity 中设置方法 myClick( )响应按钮的单击操作

代码如下

package com.example.atry;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void myClick(View view){
        Toast.makeText(MainActivity.this,"第一种监听",Toast.LENGTH_LONG).show();
    }
}

②布局界面activity_main.xml中Button的onClik属性值设为myClick

 

代码如下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="myClick"
        android:text="第一种监听"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
 

(2)通 过 按 钮 的 setOnClickListener() 方 法 注 册 监 听 事 件

在 监 听 事 件 中 创 建OnClickListener(),然后自动重写 onClick()。具体操作如下:

①布局界面activity_main.xml中添加一个Button

 代码如下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二种监听"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" /> />

</androidx.constraintlayout.widget.ConstraintLayout>

②在MainActivity中为按钮设置监听类

 

代码如下

package com.example.atry;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button2=(Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"第二种监听",Toast.LENGTH_LONG).show();
            }
        });
    }
}

(3)通过内部类实现 OnClickListener 接口,并重写 OnClick()方法

①布局界面activity_main.xml中添加一个Button 

代码如下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第三种监听"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" /> />

</androidx.constraintlayout.widget.ConstraintLayout>

②在MainActivity为按钮控件设置监听类

 

代码如下

package com.example.atry;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button3=(Button) findViewById(R.id.button3);
        button3.setOnClickListener(new MyClickListener());
    }
    class MyClickListener implements View.OnClickListener{
        @Override
        public void onClick(View v){
            Toast.makeText(MainActivity.this,"第三种监听",Toast.LENGTH_LONG).show();
        }
    }
}

(4)通过在 Activity 类实现 OnClickListener 接口

①布局界面activity_main.xml中添加一个Button

 代码如下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第四种监听"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" /> />

</androidx.constraintlayout.widget.ConstraintLayout>

②在 MainActivity 类实现 OnClickListener 接口,重写 onClick()方法

代码如下

package com.example.atry;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button3=(Button) findViewById(R.id.button4);
        button3.setOnClickListener(this);
    }

    public void onClick(View v){
        Toast.makeText(MainActivity.this,"第四种监听",Toast.LENGTH_LONG).show();
    }
}

  • RadioButton控件

单选按钮控件,继承了 Button 控件,可以直接使用 Button 控件支持的各种属性和方法。与普通按钮不同的是,RadioButton 控件多了一个可以选中的功能。通过设置 android:checked 属性可以指定初始状态是否被选中(默认为不被选中)。

具体操作如下:

①布局界面activity_main.xml中,在 RadioGroup 控件里放置RadioButton 控件

需要注意的是,RadioButton 控件必须和单选框 RadioGroup 控件一起使用

代码如下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RadioGroup
        android:id="@+id/group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="1" />
        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2" />
    </RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>

②MainActivity中通过 setOnCheckedChangeListener()方法来响应按钮的事件

 代码如下

package com.example.atry;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RadioGroup group = (RadioGroup) findViewById(R.id.group);
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton=(RadioButton) findViewById(checkedId);
                String checkText = radioButton.getText().toString();
                Toast.makeText(MainActivity.this,"选中了  "+checkText,Toast.LENGTH_LONG).show();
            }
        });
    }
}
  • CheckBox控件

CheckBox 控件是复选框控件,继承于 Button 控件,一般用于多项选中操作。其也有 android:checked 属性,可以指定初始状态时是否被选中,默认不选。

通过setOnCheckedChangeListener()方法来响应按钮的事件,具体操作如下:

①布局界面activity_main.xml中添加CheckBox控件

 代码如下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <CheckBox
        android:id="@+id/ck1"
        android:layout_width="168dp"
        android:layout_height="191dp"
        android:checked="false"
        android:text="1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.436"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="166dp" />
    <CheckBox
        android:id="@+id/ck2"
        android:layout_width="146dp"
        android:layout_height="129dp"
        android:layout_marginTop="472dp"
        android:checked="true"
        android:text="2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

②MainActivity中通过setOnCheckedChangeListener()方法来响应按钮的事件

 

代码如下

package com.example.atry;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CheckBox ck1=findViewById(R.id.ck1);
        ck1.setOnCheckedChangeListener(this);
        CheckBox ck2=findViewById(R.id.ck2);
        ck2.setOnCheckedChangeListener(this);

    }

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
            Toast.makeText(MainActivity.this, buttonView.getText()+"被选择",Toast.LENGTH_SHORT ).show();
        }
        else{
            Toast.makeText(MainActivity.this, buttonView.getText()+"取消选择",Toast.LENGTH_SHORT ).show();
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值