2015-09-01 107 views
-1

我有一个启动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> 

正如你可以看到我有ImagebuttonEdittext,我必须设置文本。

   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)); 
     } 
+0

您可以使用DialogFragment代替AlertDialog,因此您可以在片段 – httpdispatch

+1

中正确处理onActivityResult方法或者记住在显示AlertDialog时引用视图,而不是在onActivityResult方法中再次对其进行膨胀 – httpdispatch

+0

如何引用视图中的视图AlertDialog? – korunos

回答

1

所以,你需要转发被叫活动对话的结果。由于它是接收结果的调用活动,因此调用活动必须将结果传递给对话框。通常这是通过一个监听器完成的。调用活动将有一个像

interface MyActivityListener { 
    void onMyActivityResult(String s); 
}; 
MyActivityListener currentListener; 

和理想的对话框某些类型的成员要么实现此接口本身或有一个成员,

// inside the dialog class 
MyActivityListener mListener = new MyActivityListener() { 
    void onMyActivityResult(String s) { 
     ... 
    } 
} 

但你不能字段添加到,因为你的对话框使用Builder。但是你可以将对话框包装在一个类中。

// in CallingActivity: 
interface MyActivityListener { 
    void onMyActivityResult(String s); 
}; 
MyActivityListener currentListener; 

void onActivityResult(...) { 
     if (currentListener != null) { 
       currentListener.onMyActivityResult(....); 
     } 
    } 
} 

class DialogWrapper { // contains members that the dialog cannot contain (because the builder always returns an AlertDialog) 
    View rootView; 
    MyActivityListener mListener = new MyActivityListener() { 
     void onMyActivityResult(String s) { 
      ((EditText)rootView.findViewById(R.id.myeditor)).setText(s); 
      ... 
     } 
    } 

    void showDialog() { 
      AlertDialog.Builder builder = ...; 
      ... li = ...; 
      rootView = li.inflate(...); 
      builder.setView(rootView); 
      builder.setOnDismissListener(
       new ... { currentListener = null; } // !!!!! 
      ); 
      AlertDialog alertDialog = builder.create(); 
      currentListener = mListener; // !!!!! 
      alertDialog.show(); 
    } 
} 

// how to use: 
void someMethod() { 
    new DialogWrapper.showDialog(); 
} 

,或者,你可以决定,虽然所有这些听众非常洁净,这一切都归结于具有指针到根视图的活动。所以,如果你的活动是非常小的,你可以声明

// in CallingActivity 
View pointerToCurrentDialogRootView; 

,并让它在对话框的OnDismissListener设置为null。

这是一个很好的解决方案吗?那么,OOP是责任的分配,并且该活动不应该对对话的EditText做任何事情负责。 OTOH,它已经发生,它是接收对话所要求的结果的活动。我的建议是:将所有活动对对话的东西放在一个内部类中。也许,扩展DialogWrapper(上面)来完成与对话相关的所有事情,即使你决定删除监听器的东西。

相关问题