2013-01-08 70 views
1

下面的代碼用於將語音轉換爲android應用上的文本。此代碼在按下語音按鈕時起作用,您所說的任何內容都將轉換爲文本。 當語音功能關閉時,文本正在顯示。 如果再次點擊發言按鈕,則會清除上一個文本,並在頁面上顯示新文本。我想要做的是每次點擊說話按鈕,我想在頁面上顯示該單詞..然後,如果我再次單擊說話按鈕,我想要下一個單詞出現在列表格式的第一個單詞下。 我期待使用列表視圖,但我掙扎。這裏是原始代碼和我試圖編寫的代碼,使其成爲一個列表視圖。任何人可以協助?使用列表視圖將Android語音轉換爲文本

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_above="@+id/textView1" 
android:layout_toLeftOf="@+id/textView1" 
android:gravity="center" 
android:orientation="vertical" > 

<ImageButton 
    android:id="@+id/btnSpeak" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="10dp" 
    android:contentDescription="@string/speak" 
    android:src="@android:drawable/ic_btn_speak_now" /> 

<TextView 
    android:id="@+id/txtText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="10dp" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 



</LinearLayout> 

代碼:

import java.util.ArrayList; 

import android.app.Activity; 
import android.content.ActivityNotFoundException; 
import android.content.Intent; 
import android.os.Bundle; 
import android.speech.RecognizerIntent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.ImageButton; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

protected static final int RESULT_SPEECH = 1; 

private ImageButton btnSpeak; 
private TextView txtText; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    txtText = (TextView) findViewById(R.id.txtText); 

    btnSpeak = (ImageButton) findViewById(R.id.btnSpeak); 

    btnSpeak.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      Intent intent = new Intent(
        RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 

      intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US"); 

      try { 
       startActivityForResult(intent, RESULT_SPEECH); 
       txtText.setText(""); 
      } catch (ActivityNotFoundException a) { 
       Toast t = Toast.makeText(getApplicationContext(), 
         "Ops! Your device doesn't support Speech to Text", 
         Toast.LENGTH_SHORT); 
       t.show(); 
      } 
     } 
    }); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    switch (requestCode) { 
    case RESULT_SPEECH: { 
     if (resultCode == RESULT_OK && null != data) { 

      ArrayList<String> text = data 
        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 

      txtText.setText(text.get(0)); 
     } 
     break; 
    } 

    } 
} 
} 
+2

有什麼問題嗎? – clauziere

+0

對不起!應該更清楚。 當說話按鈕被按下,我說一個字,這個詞出現在屏幕上。當我再次按下說話按鈕時,第一個單詞消失,第二個單詞出現在屏幕上。我想要做的是在列表視圖中顯示屏幕上的所有單詞,並有一個清晰的按鈕,當我想要清除列表時。問題是......這樣做的代碼是什麼? – user1959064

回答

0

在你的onActivityResult,您正在使用只有第一個字(索引0)重寫你的TextView。 您必須通過字符串數組列表,像這樣重複:

String words = ""; 
    for (String word : text) { 
     words += " " + word; 
    } 
    txtText.setText(words); 

這裏是一個不錯的文章,以幫助你實現你的ListView: ListView tutorial

所以,現在當你想通了這一點,你在後面加上值列表視圖。

希望這有助於 乾杯

相關問題