我有一个AlertDialog的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Bemerkung:"
android:id="@+id/textView29" />
<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/dialogSpeechText"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingRight="34dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/ic_sirioriginalstate_teaser"
android:background="@null"
android:layout_alignRight="@+id/dialogSpeechText"
android:scaleType="fitEnd"
android:paddingRight="5dp" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
当我点击ImageButton
时,它将启动此方法:
private void promtSpeechInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 6000);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"Aufnahme...");
try {
startActivityForResult(intent, SPEECH_TO_TEXT_REQUEST);
} catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(), "Sprache nicht unterstützt", Toast.LENGTH_SHORT).show();
}
}
之后,方法onActivityResult
将被调用:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
LayoutInflater li = LayoutInflater.from(context);
View view = li.inflate(R.layout.dialog, null);
EditText text = (EditText) view.findViewById(R.id.dialog_edittext);
text.setText(text.getText() + " " + result.get(0));
}
}
修改
LayoutInflater li = LayoutInflater.from(context);
View view = li.inflate(R.layout.dialog, null);
ImageButton imageButton = (ImageButton) view.findViewById(R.id.dialog_imageButton);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(view);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
promtSpeechInput();
}
});
我在另一个Activity上使用了这些方法。现在我有一个AlertDialog并且我不知道如何将来自语音活动的文本放入警报对话框中的EditText
?