我有一个启动Google的SpeechToText对话框的ImageButton。 另外我为我的对话框使用自定义布局。如何在Android的AlertDialog中设置Edittext
<?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="wrap_content"
android:layout_marginLeft="5dp">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/dialog_edittext"
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/dialog_imagebutton"
android:src="@drawable/ic_sirioriginalstate_teaser"
android:background="@null"
android:layout_alignRight="@+id/dialog_edittext"
android:scaleType="fitEnd"
android:paddingRight="5dp" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
正如你可以看到我有Imagebutton
和Edittext
,我必须设置文本。
LayoutInflater li = LayoutInflater.from(context);
View view = li.inflate(R.layout.dialog, null);
final EditText text = (EditText) view.findViewById(R.id.dialog_edittext);
final ImageButton imageButton = (ImageButton) view.findViewById(R.id.dialog_imagebutton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
promtSpeechInput();
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(view);
AlertDialog alertDialog = builder.create();
alertDialog.show();
return true;
promptSpeechInput
方法启动语音对话框。
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(), "Test01", Toast.LENGTH_SHORT).show();
}
}
如果说了什么,onActivityResult
方法将被调用。 还有我的问题。
如何将说出的单词设置为对话框Edittext
?
我以前用过,但这次没有帮助。
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
textField.setText(textField.getText() + " " + result.get(0));
}
break;
我尽力去做我自己,但这不起作用:
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);
final EditText text = (EditText) view.findViewById(R.id.dialog_edittext);
text.setText(text.getText().toString() + " " + result.get(0));
}
您可以使用DialogFragment代替AlertDialog,因此您可以在片段 – httpdispatch
中正确处理onActivityResult方法或者记住在显示AlertDialog时引用视图,而不是在onActivityResult方法中再次对其进行膨胀 – httpdispatch
如何引用视图中的视图AlertDialog? – korunos