当前位置: 首页 > article >正文

2023TYUT移动应用软件开发程序修改题

目录

第一种监听:use the current class as the listener(重点)

第二种监听:use  a separate named class as the listener

第三种监听:use  an anonymous class as the listener

第四种监听:use  an anonymous inner class as the listener(重点)

说明:


第一种监听:use the current class as the listener(重点)

import android.view.View.OnClickListener; //第1步:导入监听器类
public class TipCalculatorActivity extends Activity implements OnClickListener{//第2.1步:实现监听器接口
    private Button percentUpButton,percentDownButton;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedlnstanceState);
        setContentView(R.layout.activity_main);
        percentUpButton=(Button)findViewByld(R.id.upbt);
        percentDownButton=(Button)findViewByld(R.id.downbt);
        percentUpButton.setOnClickListener(this);//第3步:设置监听器
        percentDownButton.setOnClickListener(this);

        @Override
        public void onClick(View v) {//第2.2步:实现监听器接口
            switch (v.getld()) {
                case R.id.percentDownButton:
                    tipPercent = tipPercent-.01f;
                    calculateAndDisplay();
                    break;
                case R.id.percentUpButton:
                    tipPercent = tipPercent+.01f;
                    calculateAndDisplay();
                    break;
            }
        }

    }
}

第二种监听:use  a separate named class as the listener

import android.view.View.OnClickListener; //第1步:导入监听器类
public class TipCalculatorActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedlnstanceState);
        setContentView(R.layout.activity_main);
        Button percentUpButton=(Button)findViewByld(R.id.upbt);
        Button percentDownButton=(Button)findViewByld(R.id.downbt);
        ButtonListener buttonListener = new ButtonListener();//第3步:创建监听实例
        percentUpButton.setOnClickListener(buttonListener);//第4步:设置监听器
        percentDownButton.setOnClickListener(buttonListener);

    }
    class ButtonListener implements OnClickListener {//第2步:编写一个实现监听器的单独类
       
@Override
        public void onClick(View v) {
            switch (v.getld()) {
                case R.id.percentDownButton:
                    tipPercent = tipPercent-.01f;
                    calculateAndDisplay();
                    break;
                case R.id.percentUpButton:
                    tipPercent = tipPercent+.01f;
                    calculateAndDisplay();
                    break;
            }
         }

    }
}

第三种监听:use  an anonymous class as the listener

import android.view.View.OnClickListener; //第1步:导入监听器类
public class TipCalculatorActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedlnstanceState);
        setContentView(R.layout.activity_main);
        Button percentUpButton=(Button)findViewByld(R.id.upbt);
        Button percentDownButton=(Button)findViewByld(R.id.downbt);
        percentUpButton.setOnClickListener(buttonListener);//第3步:设置监听器
        percentDownButton.setOnClickListener(buttonListener);

    }
    private OnClickListener buttonListener = new OnClickListener(){//第2步:为侦听器创建一个实例变量
        @Override
        public void onClick(View v) {
            switch (v.getld()) {
                case R.id.percentDownButton:
                    tipPercent = tipPercent-.01f;
                    calculateAndDisplay();
                    break;
                case R.id.percentUpButton:
                    tipPercent = tipPercent+.01f;
                    calculateAndDisplay();
                    break;
            }
         }

    }
}

第四种监听:use  an anonymous inner class as the listener(重点)

import android.view.View.OnClickListener; //第1步:导入监听器类
public class TipCalculatorActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedlnstanceState);
        setContentView(R.layout.activity_main);
        Button percentUpButton=(Button)findViewByld(R.id.upbt);
        Button percentDownButton=(Button)findViewByld(R.id.downbt);
        percentUpButton.setOnClickListener(new OnClickListener( ) {//第2.1步:设置侦听器并为侦听器实现接口
            @Override
            public void onClick(View v) {
                tipPercent = tipPercent+.01f;
                calculateAndDisplay();
            }
        });
        percentDownButton.setOnClickListener(new OnClickListener() {//第2.2步:设置侦听器并为侦听器实现接口
            @Override
            public void onClick(View v) {
                tipPercent = tipPercent-.01f;
                calculateAndDisplay();
            }
        });

    }
}

 说明:

  1. 咱们考JvaaEE时大家都做过程序修改题,这次可能不一样的是题目给你换成英文,不过都是简单词汇,肯定都能看懂
  2. 老师说会考第六章处理事件的4种监听方法,重点是6.2.1和6.2.4,也就是我下面给的第一段代码和最后一段代码,有时间都记,没时间记首尾两个
  3. 书中的代码都是片段,并不完整,但按照以往的考试经验,他会给出完整的代码让你根据要求修改其中的一部分,我将4段代码片段补充完整,并将其有区别的地方和重点进行了标注
  4. 你要想看懂代码,而不是单纯记忆,我建议去图书馆找android中文书,走马观花地过一遍,我看的是以下两本,图书馆3楼东,关于android开发的书很多
f3e8f5f723db4ac3aaac4f7918775d3e.jpeg512db38f6bf14aa8b9dc56cf6249cae0.jpeg


http://www.kler.cn/news/11934.html

相关文章:

  • C/C++程序设计——static关键字
  • NumPy 秘籍中文第二版:四、将 NumPy 与世界的其他地方连接
  • 「数字信号处理」MATLAB设计 双音多频拨号系统
  • ESP32设备驱动-MAX30102脉搏血氧饱和度和心率监测传感器驱动
  • 【GB28181】PJSIP库(七)C++接口PJSUA2类详解:Call、Buddy
  • 发令枪音效芯片,语音提示ic
  • WMS智能仓储
  • winForm目录文件介绍
  • 测试5年,从纯手工测试到测试开发,我是怎么拿到腾讯25koffer的?
  • 红黑树和平衡二叉树的优缺点及应用场景
  • ChatGPT或要推出APP,OpenAI官宣为ChatGPT招募移动端开发工程师
  • [架构之路-162]-《软考-系统分析师》-3-作系统基本原理-进程管理
  • 1.分布式电源接入对配电网影响分析
  • 追梦之旅【数据结构篇】——详解小白如何使用C语言实现堆数据结构
  • [架构之路-160]-《软考-系统分析师》-10-系统分析-7-数据与数据流程分析、需求规格说明书
  • GPT-4工具是软件工程师工作效率的倍增器
  • 前端解析Excel中的数据进行操作
  • 线性代数——矩阵
  • CTR-GCN 论文解读
  • 游戏运营专员的职责有哪些?提高游戏收入的关键是什么?
  • C语言实现惯导更新算法(机械编排)
  • DPDK入门(环境搭建以及小demo)
  • 2022国赛30:windows脚本题解析
  • python入门:cl.exe‘ failed with exit status 2错误通用解决方案
  • 【负荷预测】基于VMD-SSA-LSTM光伏功率预测【可以换数据变为其他负荷等预测】(Matlab代码实现)
  • 优橙内推河南专场——5G网络优化(中高级)工程师
  • mycat读写分离
  • C结构体中末尾的data[0]
  • Java-类的知识进阶
  • yocto 任务