編輯:關於Android編程
讓TextView和EditText發出聲音在撥打電話等應用中很常見,如果采用TTS語音引擎未免太麻煩!所以可以采用MediaPlayer播放語音文件的形式
下面是關鍵代碼:
關鍵變量定義
private TextView numInput; private int length = 0; //當前字符串長度 private int beforeLength = 0;//上一次字符串長度 private MediaPlayer mediaPlayer; private ArrayList初始化使用方法mListeners = null; EditTextWatchTEL watch;
watch = new EditTextWatchTEL(); numInput.addTextChangedListener(watch); if (mListeners == null) { mListeners = new ArrayList(); } mListeners.add(watch);
private class EditTextWatchTEL implements TextWatcher { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { if (length > 0) { if (beforeLength <= length) { beforeLength = length; List記錄與分享,讓你我共成長!歡迎查看我的其他博客;我的博客地址:http://blog.csdn.net/caicongyanglist = new ArrayList (); for(int i=0;i<10;i++){ list.add(String.valueOf(i)); } String num = numInput.getText().toString().substring(beforeLength - 1); if(list.contains(num)) { int value = Integer.parseInt(num); switch (value) { case 0: if(mediaPlayer!= null){ //一定要清空播放器資源 mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null ; } //R.raw.zero 為raw文件夾的語音文件 ,以下同上 mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.zero); mediaPlayer.start(); break; case 1: if(mediaPlayer!= null){ //一定要清空播放器資源 mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null ; } mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.one); mediaPlayer.start(); break; case 2: if(mediaPlayer!= null){ //一定要清空播放器資源 mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null ; } mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.two); mediaPlayer.start(); break; case 3: if(mediaPlayer!= null){ //一定要清空播放器資源 mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null ; } mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.three); mediaPlayer.start(); break; case 4: if(mediaPlayer!= null){ //一定要清空播放器資源 mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null ; } mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.four); mediaPlayer.start(); break; case 5: if(mediaPlayer!= null){ //一定要清空播放器資源 mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null ; } mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.five); mediaPlayer.start(); break; case 6: if(mediaPlayer!= null){ //一定要清空播放器資源 mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null ; } mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.six); mediaPlayer.start(); break; case 7: if(mediaPlayer!= null){ //一定要清空播放器資源 mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null ; } mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.seven); mediaPlayer.start(); break; case 8: if(mediaPlayer!= null){ //一定要清空播放器資源 mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null ; } mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.eight); mediaPlayer.start(); break; case 9: if(mediaPlayer!= null){ //一定要清空播放器資源 mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null ; } mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.nine); mediaPlayer.start(); break; default: break; } } } else { beforeLength = length; } } } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { length = numInput.getText().length(); } } //移除監聽 public void removeTextChangedListener(TextWatcher watcher) { if (mListeners != null) { int i = mListeners.indexOf(watcher); if (i >= 0) { mListeners.remove(i); } } numInput.removeTextChangedListener(watcher); } @Override protected void onDestroy() { removeTextChangedListener(watch); super.onDestroy(); }
一、問題描述 今天使用SDK Manager將Android SDK的版本更新到了Android 5.1的版本,eclipse創建android項目時,預覽activi
項目中常用到的圓形進度條有好多個,從網上搜到的自定義進度條多是封裝的比較好的代碼,但是不利於初學者,現在本博客就教給大家如何一步步實現自定義進度條的效果先看效果如圖&he
前言ScrollView可以說是android裡最簡單的滑動控件,但是其中也蘊含了很多的知識點。今天嘗試通過ScrollView的源碼來了解ScrollView內部的細節
最近相對來說比較閒,加上養病,所以沒事干就撸些自己之前的知識點為博客,方便自己也方便別人。1 背景之所以選擇這個知識點來分析有以下幾個原因:逛GitHub時發