2015-09-01 102 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(上面)來完成與對話相關的所有事情,即使你決定刪除監聽器的東西。

相關問題