我需要在EditText裏面的EditText的右側添加一個麥克風圖像按鈕,所以當這個圖像按鈕被擊中時,識別器將被觸發以將語音轉換爲文本。自定義EditTextPreference
回答
試試這個..
語音到文本
對於語音到文本你採取一些其他的例子......或者啓用脫機模式。
setOnTouchListener方法用於點擊你要採取正確的繪製
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class demon extends Activity {
EditText editComment;
private final int SPEECH_RECOGNITION_CODE = 1;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.demo);
editComment=(EditText) findViewById(R.id.edittext);
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0)
{
editComment.setEnabled(false);
editComment.setText("Recognizer not present");
}
editComment.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_LEFT = 0;
final int DRAWABLE_TOP = 1;
final int DRAWABLE_RIGHT = 2;
final int DRAWABLE_BOTTOM = 3;
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= (editComment.getRight() - editComment.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
// your action here
startSpeechToText();
return true;
}
}
return false;
}
});
}
private void startSpeechToText() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"Speak something...");
try {
startActivityForResult(intent, SPEECH_RECOGNITION_CODE);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
"Sorry! Speech recognition is not supported in this device.",
Toast.LENGTH_SHORT).show();
}
}
/**
* Handle the results from the voice recognition activity.
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SPEECH_RECOGNITION_CODE: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
String text = result.get(0);
editComment.setText(text);
}
break;
}
}
}
}
文本到語音
demo.java
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Locale;
public class demo extends Activity {
EditText editComment;
TextToSpeech t1;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.demo);
editComment=(EditText) findViewById(R.id.edittext);
editComment.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_LEFT = 0;
final int DRAWABLE_TOP = 1;
final int DRAWABLE_RIGHT = 2;
final int DRAWABLE_BOTTOM = 3;
if(event.getAction() == MotionEvent.ACTION_UP) {
if(event.getRawX() >= (editComment.getRight() - editComment.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
// your action here
String toSpeak = editComment.getText().toString();
Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show();
t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
return true;
}
}
return false;
}
});
t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.UK);
}
}
});
}
public void onPause(){
if(t1 !=null){
t1.stop();
t1.shutdown();
}
super.onPause();
}
}
demo.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edittext"
android:padding="10dp"
android:layout_margin="10dp"
android:drawableRight="@drawable/icon"/>
</RelativeLayout>
啓用脫機模式
http://www.androidhive.info/wp-content/uploads/2014/07/1.png
你的解決方案看起來不錯,但是當你鍵入的時候麥克風圖標消失,並出現(x)圖標,以便您可以清除文字? – Tasos
是的當然你可以改變它,當你在edittext中的值比改變右邊的繪圖editComment.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.drawableRight,0); 當值不是空的edittext比觸摸列表清除值,並改變drawale麥克風圖標,並採取代碼作爲顯示... –
對不起,英語@Tasos –
- 1. 自定義EditTextPreference
- 2. 自定義EditTextPreference和setOnPreferenceChangeListener未調用
- 3. Android自定義EditTextPreference UI沒有得到更新
- 4. EditTextPreference
- 5. 可能自動完成EditTextPreference?
- 6. 自定義EditTextPreference。如何以及何時爲保存目的獲取文本
- 7. 如何創建僅接受預定義範圍值的EditTextPreference?
- 8. 「就地」EditTextPreference
- 9. 驗證editTextPreference Android
- 10. Android EditTextPreference風格
- 11. 閱讀EditTextPreference
- 12. EditTextPreference中的setImeOptions
- 13. EditTextPreference崩潰
- 14. 更改EditTextPreference
- 15. android EditTextPreference onclick
- 16. setInputType上EditTextPreference
- 17. 一個多EditTextPreference
- 18. 設置EditTextPreference高度
- 19. EditTextPreference使用按鈕
- 20. EditTextPreference禁用按鈕?
- 21. 分配EditTextPreference inputType「number」
- 22. EditTextPreference不能設置
- 23. 自定義類=自定義+分配,自定義+初始化
- 24. 自定義UIView自定義UITableViewCell
- 25. 自定義控件自定義方法?
- 26. 自定義newsbsite.aspx和自定義webtmplates(sharePoint)
- 27. 自定義與非自定義屬性?
- 28. 自定義ArrayAdapter自定義過濾器
- 29. 自定義UITextview裏面自定義UITableviewcell
- 30. 自定義ListView - 自定義參數
只(機器人:layout_toLeftOf = 「@ + ID /麥克風」),並添加一些左填充或保證金您EDITTEXT - 所以它的(Edittext ---麥克風圖標)如果你還沒有在麥克風後面有更多的圖標,然後添加(android:layout_alignParentRight =「true」)和一些邊緣或填充麥克風圖標的權利 - 應該對齊他們好吧,即時消息使用這種方法 – Tasos