zoukankan      html  css  js  c++  java
  • 5月29日学习日志

    今天学习了Vibrator(振动器)。

    简单的布局文件,五个按钮:activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        
        <Button
            android:id="@+id/btn_hasVibrator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="判断是否有振动器" />
    
    
        <Button
            android:id="@+id/btn_short"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="短振动" />
    
        <Button
            android:id="@+id/btn_long"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="长振动" />
    
        <Button
            android:id="@+id/btn_rhythm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="节奏振动" />
    
        <Button
            android:id="@+id/btn_cancle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="取消振动" />
    </LinearLayout>

    接着是MainActivity.java部分:

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private Button btn_hasVibrator;
        private Button btn_short;
        private Button btn_long;
        private Button btn_rhythm;
        private Button btn_cancle;
        private Vibrator myVibrator;
        private Context mContext;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //获得系统的Vibrator实例:
            myVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
            mContext = MainActivity.this;
            bindViews();
        }
    
        private void bindViews() {
            btn_hasVibrator = (Button) findViewById(R.id.btn_hasVibrator);
            btn_short = (Button) findViewById(R.id.btn_short);
            btn_long = (Button) findViewById(R.id.btn_long);
            btn_rhythm = (Button) findViewById(R.id.btn_rhythm);
            btn_cancle = (Button) findViewById(R.id.btn_cancle);
    
            btn_hasVibrator.setOnClickListener(this);
            btn_short.setOnClickListener(this);
            btn_long.setOnClickListener(this);
            btn_rhythm.setOnClickListener(this);
            btn_cancle.setOnClickListener(this);
        }
    
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_hasVibrator:
                    Toast.makeText(mContext, myVibrator.hasVibrator() ? "当前设备有振动器" : "当前设备无振动器",
                            Toast.LENGTH_SHORT).show();
                    break;
                case R.id.btn_short:
                    myVibrator.cancel();
                    myVibrator.vibrate(new long[]{100, 200, 100, 200}, 0);
                    Toast.makeText(mContext, "短振动", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.btn_long:
                    myVibrator.cancel();
                    myVibrator.vibrate(new long[]{100, 100, 100, 1000}, 0);
                    Toast.makeText(mContext, "长振动", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.btn_rhythm:
                    myVibrator.cancel();
                    myVibrator.vibrate(new long[]{500, 100, 500, 100, 500, 100}, 0);
                    Toast.makeText(mContext, "节奏振动", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.btn_cancle:
                    myVibrator.cancel();
                    Toast.makeText(mContext, "取消振动", Toast.LENGTH_SHORT).show();
            }
        }
    }
  • 相关阅读:
    [daily][archlinux][shell][fish] 使用最炫酷的shell工具fish
    [daily][btrfs][mlocate][updatedb] mlocate不认识btrfs里面的文件
    [daily][archlinux] TODO LIST
    [math][mathematica] mathematica入门
    [math][mathematica] archlinux 下 mathematica 的安装 (科学计算软件 mathematica/matlab/sagemath)
    [daily][centos][sudo] sudo 报错
    [development][C][thread_local] 线程全局变量
    [development][C] C语言标准
    [daily][centos][nginx] 在centos7使用nginx启用对文件目录的http访问
    CS RANK: AI & ML
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14910718.html
Copyright © 2011-2022 走看看