午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Android振動(dòng)器使用方法詳解

瀏覽:11日期:2023-12-08 15:25:55

本文實(shí)例為大家分享了Android振動(dòng)器使用方法的具體代碼,供大家參考,具體內(nèi)容如下

效果圖:

Android振動(dòng)器使用方法詳解

選擇相應(yīng)的毫秒數(shù),就會(huì)振動(dòng)相應(yīng)的秒數(shù)。

實(shí)現(xiàn)步驟:

一、創(chuàng)建activity_vibrator.xml布局

<?xml version='1.0' encoding='utf-8'?><LinearLayout 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' tools:context='.VibratorActivity' android:orientation='vertical' > <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:orientation='horizontal'><TextView android:layout_width='0dp' android:layout_height='wrap_content' android:layout_weight='1' android:text='振動(dòng)時(shí)長(zhǎng):' android:textSize='15sp' android:textColor='@color/black' android:paddingLeft='5dp' /><Spinner android: android:layout_width='0dp' android:layout_height='wrap_content' android:layout_weight='3' android:paddingTop='5dp' android:spinnerMode='dialog' /> </LinearLayout> <Buttonandroid: android:layout_width='match_parent'android:layout_height='wrap_content'android:text='開始振動(dòng)'android:textColor='@color/black'android:textSize='20sp'/> <TextViewandroid: android:layout_width='match_parent'android:layout_height='wrap_content'android:textSize='20sp'android:gravity='center'android:text='當(dāng)前振動(dòng)了多長(zhǎng)時(shí)間'/></LinearLayout>

之后繪制,下拉列表,每一列的高度和每一列中字體的顏色和太小等屬性在這里面設(shè)置

item_select.xml布局如下:

<TextView xmlns:android='http://schemas.android.com/apk/res/android' android: android:layout_width='match_parent' android:layout_height='60dp' android:gravity='center' android:textColor='@color/black' android:textSize='20sp' />

之后在VibratorActivity中實(shí)現(xiàn)振動(dòng)功能:

public class VibratorActivity extends AppCompatActivity implements View.OnClickListener { private Spinner spinner; private TextView tv_specific; private Button btn_start; private ArrayAdapter<String> arrayAdapter; private String second; private Vibrator vibrator; private int mDuration; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_vibrator);spinner = findViewById(R.id.spinner);tv_specific = findViewById(R.id.tv_specific);btn_start = findViewById(R.id.btn_start);//設(shè)置下拉框CreateSpinner();btn_start.setOnClickListener(this); } private void CreateSpinner() {String[] array = new String[]{'0.5秒', '1秒', '2秒', '3秒', '4秒', '5秒'};int[] durationArray = new int[]{500, 1000, 2000, 3000, 4000, 5000};//設(shè)置我們自定義的資源樣式arrayAdapter = new ArrayAdapter<>(this, R.layout.item_select, array);spinner.setPrompt('請(qǐng)選擇毫秒數(shù)');//將適配器與下拉列表框關(guān)聯(lián)起來spinner.setAdapter(arrayAdapter);spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {mDuration = durationArray[position]; } @Override public void onNothingSelected(AdapterView<?> parent) { }}); } @Override public void onClick(View v) {String vibratorService = Context.VIBRATOR_SERVICE;//從系統(tǒng)服務(wù)中獲取振動(dòng)管理器vibrator = (Vibrator) getSystemService(vibratorService);//判斷設(shè)置是否包含振動(dòng)器if (vibrator.hasVibrator()) { //振動(dòng)的秒數(shù) vibrator.vibrate(mDuration); String desc = String.format('%s手機(jī)振動(dòng)了%f秒', DateUtil.getNowTimeDetail(), mDuration / 1000.0F); tv_specific.setText(desc);} } //應(yīng)用退出,則取消振動(dòng) @Override protected void onDestroy() {super.onDestroy();vibrator.cancel(); }}

最后不要忘了在AndroidManifest.xml清單文件中加入控制設(shè)備振動(dòng)的權(quán)限:

<!-- 振動(dòng)權(quán)限 --><uses-permission android:name='android.permission.VIBRATE' />

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 蜜桃在线一区二区 | 黄色免费看网站 | 蜜臀久久99精品久久久久宅男 | 天堂俺去俺来也www久久婷婷 | 久操资源在线 | 肉大捧一出免费观看网站在线播放 | 精品一区二区成人免费视频 | 日韩激情视频在线观看 | 骚鸭av | 久久国产麻豆 | 91久久国产综合 | 女人洗澡一级特黄毛片 | 成年人免费视频播放 | 日韩精品免费一区二区三区 | 色天天综合 | 福利一二区 | 国产精品1234区 | 综合婷婷 | 成人免费看片39 | 欧美资源网 | 国产午夜在线视频 | 一本一道精品欧美中文字幕 | 91国产丝袜播放在线 | 欧美国产一级 | 国产精品www| 日日碰碰| 一级特黄色大片 | 天堂一区 | 成人一区二区在线观看 | 999国产精品 | 日韩视频免费在线观看 | 91亚洲精品视频 | 亚洲精品乱码久久久久久日本蜜臀 | 亚洲黄色av | 日韩高清av在线 | 久久久久久97| 成人精品av | 亚洲a毛片 | 爽天天天天天天天 | 日韩欧美中文字幕在线视频 | 精品久久久在线观看 |