安卓基础(点击事件)

摘要:
今天,我了解了Android开发的点击事件。让我描述一下实现点击时间的基本方法。首先,将onclick属性格式添加到要单击的控件:android:onclick=“methodname”。例如,android:onClick=“oneClick”。接下来,在与此布局相对应的活动中编写一个方法。格式为:publicvoid方法名称(Viewview){方法执行代码;}例如:publicvoioneOnclick(V

今天学习了安卓开发的点击事件,下面我来叙述一下实现点击时间得基本方法:

第一种:在要被点击得控件中加onclick属性

格式:android:onClick="方法名" 例如andoroid:onClick="oneClick"

接下来在对应这个布局得activity中写一个方法,格式为:

public void 方法名(View view ){

方法得执行代码;

例如:

public void oneOnclick(View view){
Log.d(TAG,"...1");
if(view instanceof TextView){
String a = ((TextView)view).getText().toString();
Log.d(TAG,"name =="+ a);//打印每个键所包含得文本内同容
    }
}
第一种实例(计算器)代码:
activity代码:
package com.example.text2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculatorlayout);
    }
public void oneOnclick(View view){
    Log.d(TAG,"...1");
    if(view instanceof TextView){
        String a = ((TextView)view).getText().toString();
        Log.d(TAG,"name =="+ a);//打印每个键所包含得文本内同容
    }
}
}

xml文件带代码:

<?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"

    android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_gravity="center"
    android:orientation="horizontal"
    >
    <TextView
        android:onClick="oneOnclick"
        android:layout_width="0dp"
android:layout_height="match_parent"
         android:text="C"
        android:gravity="center"
        android:textSize="30sp"
        android:background="@drawable/selector"
        android:layout_weight="1"></TextView>
    <TextView
        android:onClick="oneOnclick"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="+/-"
       android:gravity="center"
        android:textSize="30sp"
        android:background="@drawable/selector"
        android:layout_weight="1"></TextView>
    <TextView
        android:onClick="oneOnclick"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="%"
       android:gravity="center"
        android:textSize="30sp"
        android:background="@drawable/selector"
        android:layout_weight="1"></TextView>
    <TextView
        android:onClick="oneOnclick"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="÷"
        android:gravity="center"
        android:textSize="30sp"
        android:background="@drawable/selector_oring"

        android:layout_weight="1"></TextView>

</LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_gravity="center"
        android:orientation="horizontal"
        >
        <TextView
            android:onClick="oneOnclick"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="1"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector"
            android:layout_weight="1"></TextView>
        <TextView
            android:onClick="oneOnclick"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="2"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector"
            android:layout_weight="1"></TextView>
        <TextView
            android:onClick="oneOnclick"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="3"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector"
            android:layout_weight="1"></TextView>
        <TextView
            android:onClick="oneOnclick"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="÷"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_oring"

            android:layout_weight="1"></TextView>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_gravity="center"
        android:orientation="horizontal"
        >
        <TextView
            android:onClick="oneOnclick"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="4"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector"
            android:layout_weight="1"></TextView>
        <TextView
            android:onClick="oneOnclick"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="5"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector"
            android:layout_weight="1"></TextView>
        <TextView
            android:onClick="oneOnclick"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="6"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector"
            android:layout_weight="1"></TextView>
        <TextView
            android:onClick="oneOnclick"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="x"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_oring"

            android:layout_weight="1"></TextView>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_gravity="center"
        android:orientation="horizontal"
        >
        <TextView
            android:onClick="oneOnclick"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="7"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector"
            android:layout_weight="1"></TextView>
        <TextView
            android:onClick="oneOnclick"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="8"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector"
            android:layout_weight="1"></TextView>
        <TextView
            android:onClick="oneOnclick"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="9"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector"
            android:layout_weight="1"></TextView>
        <TextView
            android:onClick="oneOnclick"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="-"
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_oring"
            android:layout_weight="1"></TextView>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_gravity="center"
        android:orientation="horizontal"
        >
        <TextView
            android:onClick="oneOnclick"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="0"
            android:gravity="center"
            android:textSize="30sp"
            android:layout_weight="2"
            android:background="@drawable/selector"
            ></TextView>
        <TextView
            android:onClick="oneOnclick"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="."
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector"
            android:layout_weight="1"></TextView>

        <TextView
            android:onClick="oneOnclick"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="="
            android:gravity="center"
            android:textSize="30sp"
            android:background="@drawable/selector_oring"
            android:layout_weight="1"></TextView>

    </LinearLayout>
</LinearLayout>

白色外边框代码:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#ffffff"/>
    <stroke android:color="#333333" android: />
</shape>

点击之后变色代码:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#999999"/>
    <stroke android:color="#333333" android: />
</shape>

橙色外边框代码:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#e47f24"/>
    <stroke android:color="#333333" android: />
</shape>

点击之后变色代码:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#b55f1c"/>
    <stroke android:color="#333333" android: />
</shape>

白色部分选择事件代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shape_rectangle_grey" android:state_pressed="true"/>
    <item android:drawable="@drawable/shape_rectangle"/>
</selector>

橙色部分选择事件代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/shape_rectangle_oring_gery"/>
    <item android:drawable="@drawable/shape_rectangle_oring"/>
</selector>

安卓手机端开发截图:

安卓基础(点击事件)第1张

点击控件,显示台截图:

安卓基础(点击事件)第2张

第二种:通过id声明来找到控件并对其进行设置点击事件

第一步:给对应的控件设置id

第二步:找到控件

第三步:设置点击事件

package com.example.text2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
    private TextView cancel;
    private TextView plus_red;
    private TextView divisible;
    private TextView chu;
    private TextView one;
    private TextView two;
    private TextView three;
    private TextView plus;
    private TextView four;
    private TextView five;
    private TextView six;
    private TextView multiply;
    private TextView senven;
    private TextView eight;
    private TextView nine;
    private TextView cut;
    private TextView zero;
    private TextView spot;
    private TextView equal;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculatorlayout_byid);
        initview();
        //设置点击事件
        initClick();
    }

    private void initview() {
         cancel = this.findViewById(R.id.cancel);
         plus_red = this.findViewById(R.id.plus_red);
        divisible = this.findViewById(R.id.divisible);
        chu = this.findViewById(R.id.chu);
        one = this.findViewById(R.id.one);
        two = this.findViewById(R.id.two);
        three = this.findViewById(R.id.three);
        plus = this.findViewById(R.id.plus);
        four = this.findViewById(R.id.four);
        five = this.findViewById(R.id.five);
        six = this.findViewById(R.id.six);
        multiply = this.findViewById(R.id.multiply);
        senven = this.findViewById(R.id.senven);
        eight = this.findViewById(R.id.eight);
        nine = this.findViewById(R.id.nine);
        cut = this.findViewById(R.id.cut);
        zero = this.findViewById(R.id.zero);
        spot = this.findViewById(R.id.spot);
        equal = this.findViewById(R.id.equal);
    }
/*
设置点击事件
 */
    public  void  initClick(){
        //第一种设置方式
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"点击键值为"+ ((TextView)v).getText().toString());
            }
        });
        divisible.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"点击键值为"+ ((TextView)v).getText().toString());
            }
        });
        chu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"点击键值为"+ ((TextView)v).getText().toString());
            }
        });
        plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"点击键值为"+ ((TextView)v).getText().toString());
            }
        });
        four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"点击键值为"+ ((TextView)v).getText().toString());
            }
        });
        five.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"点击键值为"+ ((TextView)v).getText().toString());
            }
        });
        six.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"点击键值为"+ ((TextView)v).getText().toString());
            }
        });

//第二中设置方法
         plus_red.setOnClickListener(this);
         one.setOnClickListener(this);
         two.setOnClickListener(this);
         three.setOnClickListener(this);
        multiply.setOnClickListener(this);
        senven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        cut.setOnClickListener(this);
        zero.setOnClickListener(this);
        spot.setOnClickListener(this);
        equal.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Log.d(TAG,"点击键值为"+ ((TextView)v).getText().toString());
    }
}

安卓开发截图和第一种方法一样就不截图了,控制台截图(为方便区分我输出值为中文):

安卓基础(点击事件)第3张

这是今天所学,明天继续加油,丰富自己的知识储备!

 

免责声明:文章转载自《安卓基础(点击事件)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇VC++ MFC CheckBoxpython之tkinter使用-多选框实现开关操作下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Chrome 扩展crx开发

Chrome扩展提供的入口 左键 crx,popup 右键 crx,homelink + option 右键上下文菜单 Chrome扩展的文件结构 Chrome扩展就是一个文件夹下包括一堆符合规范的文件。首先是清单文件manifest.json,指定了该扩展的整体布局和结构。实例: { // 清单文件的版本,这个必须写,而且必须是2 "mani...

最简单的Android项目

这是我在windows环境下,试验过的最简单Android项目,只用记事本和命令行即可完成。 环境准备 开发环境需要Java SDK(官网下载),Android SDK(官网下载)。 首先安装Java SDK,然后将Android SDK解压到任意目录(建议解压到某个盘根目录,方便后续命令输入)。 接着需要下载Android SDK。打开刚才解压目录下的S...

快到极致的Android模拟器——Genymotion

转载声明:Ryan的博客文章欢迎您的转载,但在转载的同时,请注明文章的来源出处,不胜感激! :-) http://my.oschina.net/ryanhoo/blog/141824 还在用Android原生模拟器? 给你推荐一款全方位把Android原生模拟器秒成渣渣的神器:Genymotion! 需要理由? 性能卓越 作为历史上最快的Androi...

NDK笔记(二)-在Android Studio中使用ndk-build(转)

前面一篇我们接触了CMake,这一篇写写关于ndk-build的使用过程。刚刚用到,想到哪儿写哪儿。 环境背景 Android开发IDE版本:AndroidStudio 2.2以上版本(目前已经升级到2.2.3) 计算机环境:Window 64位系统 Demo工程的创建与设置 1.新建一个工程,并在app/src/main路径下新建jni文件夹。 2.在...

Python实现Windows CMD命令行彩色输出

#! /usr/bin/env python #coding=utf-8   importctypes,sys   STD_INPUT_HANDLE =-10 STD_OUTPUT_HANDLE =-11 STD_ERROR_HANDLE =-12   # 字体颜色定义 ,关键在于颜色编码,由2位十六进制组成,分别取0~f,前一位指的是背景...

使用swiper.js实现移动端tab切换

在项目中遇到的,要实现tab切换,我用的是swiper.js 官网:http://www.swiper.com.cn/api/start/new.html 1 <!DOCTYPE html> 2 <html lang="zh"> 3 <head> 4 <meta charset="UTF-...