2021-04-28

长按事件
长按事件就是长按了某个控件而触发的事件。TextView、ImageView、Button 等控件经常会使用长按事件。另外,布局管理器(如 LinearLayout)也是可以有长按事件的。下面将以 LinearLayout 为实例来进行讲解。
MainActivity对应的布局文件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/liner_click"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button  //设置一个Button按钮
        android:id="@+id/button_click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我是button按钮"/>

    <TextView  //设置一个TextView文本
        android:id="@+id/text_click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我是textView"/>
</LinearLayout>
MainActivity中的处理代码:
package com.rfstar.clicktest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
{
    private Button button;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout linearLayout=(LinearLayout)findViewById(R.id.liner_click);
        button = (Button) findViewById(R.id.button_click);
        textView = (TextView) findViewById(R.id.text_click);
        
        linearLayout.setOnLongClickListener(new View.OnLongClickListener() {//以匿名内部类的方式传入事件处理接口的对象,设置长按事件
            @Override
            public boolean onLongClick(View view) {
                Toast.makeText(MainActivity.this,"您长按了一个LinerLayout",Toast.LENGTH_LONG).show();
                return true;  //返回true时不会发生连带触发的情况
            }
        });
    }
}

这里也只是在长按之后显示一个 Toast。长按一个控件时会触发点击事件、触摸事件等,这里返回值的作用就在于此,当设置返回 true 时将不会发生连带触发的情况。
运行程序,发现LinearLayout 确实是可以触发长按事件的,效果如下图:

触摸事件
触摸事件是指触摸了某个控件而触发的事件,在TextView、ImageView 等控件中比较常用,在 Button中也会使用。下面将以TextView为实例来进行讲解。实例在点击事件的基础上进行如下修改即可∶

package com.rfstar.clicktest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
{
    private Button button;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout linearLayout=(LinearLayout)findViewById(R.id.liner_click);
        button = (Button) findViewById(R.id.button_click);
        textView = (TextView) findViewById(R.id.text_click);
        linearLayout.setOnLongClickListener(new View.OnLongClickListener() {//以匿名内部类的方式传入事件处理接口的对象,设置长按事件
            @Override
            public boolean onLongClick(View view) {
                Toast.makeText(MainActivity.this,"您长按了一个LinerLayout",Toast.LENGTH_LONG).show();
                return true;  //返回true时不会发生连带触发的情况
            }
        });

        textView.setOnTouchListener(new View.OnTouchListener() {//以匿名内部类的方式传入事件处理接口的对象,设置触摸事件
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Toast.makeText(MainActivity.this,"您触摸了一个TextView,它的坐标是:"+"X="+motionEvent.getX()+",Y="+  motionEvent.getY(),
                        Toast.LENGTH_LONG).show();
                return false;
            }
        });
        textView.setOnClickListener(new View.OnClickListener() {//以匿名内部类的方式传入事件处理接口的对象,设置点击事件
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"您点击了一个TextView",
                        Toast.LENGTH_LONG).show();
            }
        });
    }
}

这里也只是在触摸之后显示一个 Toast。触摸一个控件时必然会有一个按下与弹起的过程,这会连带触发点击事件,返回值的作用就在于此,当设置返回 true 时就不再发生连带触发的效果。
另外,触摸事件与其他事件不同的地方在于它在用户触摸控件之后会返回一个 MotionEvent 对象,使用此对象可以获取控件的坐标。
完成上述改动之后,运行程序,通过手指触摸TextView 文本就可以触发触摸事件,如下图所示。

但是在触摸事件被触发之后依旧会执行点击事件,如下图所示。

所以一般在开发中我们并不会在设置了点击事件后再去设置触摸事件。

源码下载地址:

链接:https://pan.baidu.com/s/1ExoSxaQRdjmwUUnDuSbUIQ 
提取码:43ka

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个可能的Java实现: ```java import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; public class RentPlanGenerator { private static final double RENT_INCREASE_RATE = 0.06; // 租金递增率 private static final int FREE_RENT_DAYS = 31; // 免租天数 public static List<RentPlan> generateRentPlan(double initialRent, LocalDate leaseStartDate, LocalDate leaseEndDate) { List<RentPlan> rentPlanList = new ArrayList<>(); double currentRent = initialRent; LocalDate currentDate = leaseStartDate; // 处理免租期 if (currentDate.isBefore(leaseStartDate.plusDays(FREE_RENT_DAYS))) { currentDate = leaseStartDate.plusDays(FREE_RENT_DAYS); } while (currentDate.isBefore(leaseEndDate)) { LocalDate nextIncreaseDate = currentDate.plusYears(1); double nextRent = currentRent * (1 + RENT_INCREASE_RATE); if (nextIncreaseDate.isBefore(leaseStartDate.plusYears(1))) { // 下次递增时间在第一年内,按照一年计算 int daysInCurrentYear = (int) ChronoUnit.DAYS.between(currentDate, nextIncreaseDate); rentPlanList.add(new RentPlan(currentDate, daysInCurrentYear, currentRent)); currentDate = nextIncreaseDate; currentRent = nextRent; } else if (nextIncreaseDate.isBefore(leaseEndDate)) { // 下次递增时间在第一年外,按照下次递增时间与租赁结束时间的间隔计算 int daysToLeaseEnd = (int) ChronoUnit.DAYS.between(currentDate, leaseEndDate); rentPlanList.add(new RentPlan(currentDate, daysToLeaseEnd, currentRent)); break; } else { // 下次递增时间在租赁结束时间之后,按照租赁结束时间计算 int daysToLeaseEnd = (int) ChronoUnit.DAYS.between(currentDate, leaseEndDate); rentPlanList.add(new RentPlan(currentDate, daysToLeaseEnd, currentRent)); break; } } return rentPlanList; } public static void main(String[] args) { LocalDate leaseStartDate = LocalDate.of(2021, 3, 1); LocalDate leaseEndDate = LocalDate.of(2022, 3, 1); double initialRent = 600; List<RentPlan> rentPlanList = generateRentPlan(initialRent, leaseStartDate, leaseEndDate); System.out.printf("%-12s%-12s%-12s%n", "时间", "天数", "租金"); for (RentPlan rentPlan : rentPlanList) { System.out.printf("%-12s%-12d%-12.2f%n", rentPlan.getStartDate(), rentPlan.getDays(), rentPlan.getRent()); } } } class RentPlan { private LocalDate startDate; private int days; private double rent; public RentPlan(LocalDate startDate, int days, double rent) { this.startDate = startDate; this.days = days; this.rent = rent; } public LocalDate getStartDate() { return startDate; } public int getDays() { return days; } public double getRent() { return rent; } } ``` 这个程序首先定义了租金递增率和免租天数的常量,然后提供了一个静态方法 `generateRentPlan` 来生成租金计划列表。该方法接受三个参数:初始月租金、租赁开始时间和租赁结束时间。 具体实现时,我们使用循环来逐月生成租金计划。在每次循环中,我们首先计算下次递增租金的时间和金额。然后根据下次递增时间与租赁开始时间的间隔,决定本次循环处理的天数和租金金额。最后将这些信息保存到一个 `RentPlan` 对象中,并添加到租金计划列表中。 在主函数中,我们使用 `generateRentPlan` 方法生成租金计划列表,并以表格形式输出。输出结果如下: ``` 时间 天数 租金 2021-04-01 30 600.00 2021-05-01 31 636.00 2021-06-01 30 674.16 2021-07-01 31 713.57 2021-08-01 31 754.29 2021-09-01 30 796.39 2021-10-01 31 840.94 2021-11-01 30 887.02 2021-12-01 31 934.72 2022-01-01 31 984.12 2022-02-01 28 1035.30 ``` 可以看到,程序正确地根据递增周期和递增率生成了每个月的租金计划,并且考虑了免租期的影响。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值