如何在android中的编辑文本字段中显示结果

时间:2013-03-09 10:42:31

标签: android android-edittext

我正在开发一个用于发送带有语音识别界面的短信的Android应用程序。我正面临着从语音识别界面显示结果的问题。我无法在消息列(编辑文本)中显示结果(转换后的文本)。但是当我在窗口中提供文本视图时,我可以在文本字段中显示结果。所以请帮助我在编辑文本字段中获得结果。

CODES

 package com.voice.sms;

 import android.app.Activity;
 import android.content.Intent;
 import android.content.ActivityNotFoundException;
 import android.os.Bundle;
 import android.telephony.SmsManager;
 import android.speech.RecognizerIntent;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.ImageButton;
 import android.widget.TextView;
 import java.util.ArrayList;
 import android.widget.Toast;

public class SendsmsActivity extends Activity {

protected static final int RESULT_SPEECH = 0;
Button btnsend;
ImageButton btnspk;
EditText tRec;
EditText tMsg;
TextView ttxt;
String tstrg;

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

    btnsend = (Button) findViewById(R.id.btnsend);
    btnspk = (ImageButton) findViewById(R.id.imgbtnspk);
    ttxt = (TextView) findViewById(R.id.txt1);

    btnsend.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            tRec = (EditText) findViewById(R.id.etxtrec);
            tMsg = (EditText) findViewById(R.id.etextmsg);
            sendSMS(tRec.getText().toString(), tMsg.getText().toString());
        }
    });

    btnspk.setOnClickListener(new View.OnClickListener() {
        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);
                ttxt.setText("");
            } catch (ActivityNotFoundException a) {
                Toast t = Toast.makeText(getApplicationContext(),
                        "Opps! Your device doesn't support Speech to Text",
                        Toast.LENGTH_SHORT);
                t.show();
            }
        }
    });

}`




// ---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message) {
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);
}


@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);

            //String spchtxt = text.get(0).toString();
            ttxt.setText(text.get(0));



        }
        break;
    }

    }


}

}

sendsms.xml

`<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context=".sendsms"
tools:ignore="ExtraText" >

<TextView
    android:id="@+id/txtrec"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="23dp"
    android:text="@string/rec"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/etxtrec"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/txtrec"
    android:ems="10"

    android:inputType="phone" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/txtmsg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/etxtrec"
    android:layout_marginTop="34dp"
    android:text="@string/msg"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/btnsend"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:text="@string/snd" />

<ImageButton
    android:id="@+id/imgbtnspk"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btnsend"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:src="@android:drawable/ic_btn_speak_now" 
    android:contentDescription="@string/spk"/>

<AutoCompleteTextView
    android:id="@+id/etextmsg"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/txtmsg"
    android:ems="20"
    android:gravity="top" />

<TextView
    android:id="@+id/txt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/imgbtnspk"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/etextmsg"
    android:layout_marginBottom="16dp" 
  />

   </RelativeLayout>`

现在我可以在txt1(文本视图字段)中显示结果(文本)。但我想在文本视图中显示结果

1 个答案:

答案 0 :(得分:0)

试试这个:

EditText editText = (EditText)findViewById(R.id.edit_text);
editText.setText("Google is your friend.", TextView.BufferType.EDITABLE);