Day27 Android - onTouch+SharePreferences

今天主要要来谈谈两个功能:onTouch(点击事件)、SharePreferences(用於储存资料),不要问我为什麽会把这两个应用合在一天讲(X,首先布局的部分我放了一个TextView来显示点击的次数、而按钮用来清空次数,那麽就先从onTouch开始讲,首先来让主程序实作(implements)View.OnTouchListener的介面(interface)。
https://ithelp.ithome.com.tw/upload/images/20210908/20139259C6y8OLlN3s.png
而在实作这个介面的时候会产生红字,是因为这个介面里有个onTouch方法需要去定义,接着就按alt+enter->implements methods。


https://ithelp.ithome.com.tw/upload/images/20210908/20139259J6GGKJ9zXc.png
实作onTouch方法,点选OK就会实作出以下的程序。

@Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }

这个地方等等会做比较多操作的是event,view只需要在onCreate内设定setOnTouchListener用onTouch这个方法,MotionEvent主要是存放了触控动作、和一些触控的方法,举几个常用的:

触发动作

  • MotionEvent.ACTION_DOWN : 检测到触点时触发的事件
  • MotionEvent.ACTION_UP : 放开触点时触发的事件
  • MotionEvent.ACTION_MOVE : 移动触点时触发的事件

方法

  • event.getAction() :取得动作型别
  • event.getX() :返回当前x的相对座标 , event.getY() :返回当前y的相对座标
  • event.getDownTime() :按下的开始时间
  • event.getEventTime() :事件结束的时间

当然还有其他更广的应用,像是滑鼠的滚轮等,更多请参考:Android Developers/MotionEvent


那麽接着来做设计的部分:

public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
//此为宣告的部分
    TextView textView;//显示按下次数
    Button button;//清零按钮
    int i;//按下次数
    SharedPreferences save;//存值
    SharedPreferences.Editor editor;//存值的方法
//实作介面的onTouch方法
public boolean onTouch(View v, MotionEvent event) 
    {
        //Vibrator宣告,Vibrator为震动器
        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

        if(event.getAction() == MotionEvent.ACTION_DOWN){ //取得动作==检测到触点时(点击萤幕)
            vibrator.vibrate(1000); //震动多久(ms)
            i=i+1;//点击次数
            return true;
        }
        else if(event.getAction() == MotionEvent.ACTION_UP){ //取得动作==放开触点时(放开萤幕)
            vibrator.cancel(); //停止震动
            return false;
        }
        textView.setText(""+i);//显示(此findViewById写在onCreate内)
        return false;
    }
    //onCreate...
    

这边我设计了一个变数i来储存我按了多少下(会放进SharePreferences存)、以及震动器Vibrator的使用,那麽接着就往下看,以及SharePreferences怎麽进行储存值及取值的部分:

//...
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=findViewById(R.id.button);
        save = getSharedPreferences("saveName",MODE_PRIVATE);
        i = save.getInt("i",0);//取得editor放进去的i资料
        textView = findViewById(R.id.textview);
        if(i>0){
            textView.setText(""+i);
        }
        View view = findViewById(R.id.activity);//在最外层布局中加入的id来绑定
        view.setOnTouchListener(this::onTouch);//设定setOnTouchListener使用onTouch方法
    }
    public void click(View v){
        i=0;//清零
    }
    @Override
    protected void onStop()
    {
        super.onStop();
        Log.d("Tag_stop",""+i);//检测关闭时是否有写入数值
        save = getSharedPreferences("saveName",MODE_PRIVATE);
        editor = save.edit();//编辑save物件
        editor.putInt("i", i);//放入i值至"i"这个标签中
        editor.commit();//储存
    }

布局

<?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"
    android:id="@+id/activity"
    tools:context=".MainActivity">
<!--在最外层布局透过android:id加入id值以绑定-->
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="528dp"
        android:text="RESET"
        android:onClick="click"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这样就完成了!


<<:  [DAY-28] 写下你的故事 :)

>>:  Day30 下拉式选单小实作2

网路的小技巧-3

//兴趣记录一下~希望退休以後可以回味,各位别嫌弃,感谢各位!! /// //三家分公司互连 图(一...

[面试][人格特质]一再被问的经典面试题

每个人的性格不同,不要去追求所谓的完美解答;而是去寻找适合自己的环境。 这边笔者依照类型统整了经常...

[Day 4] 来个简单的小应用呗~

前几天教了一些基础概念和如何安装,今天就来个小应用来告诉各位要如何实际应用吧~! 首先创一个.htm...

[很不铁人的 IT 铁人赛] 前端工程师学资讯安全

(图片来自 Google for Education) 2021 铁人赛又开赛啦 ! 继 2019...

Day 6 分好分清楚!

每天当我们张开眼的时候,世界就井然有序在运作了,只是我们在某个轨道上运行太久了,久到没有发现已经在轨...