自定义EditTextPreference

时间:2016-08-31 04:08:05

标签: android android-layout

我需要在EditTextPreference内的EditText右侧添加一个麦克风图像按钮,这样当点击此图像按钮时,将触发识别器将语音转换为文本。

1 个答案:

答案 0 :(得分:1)

试试这个..

  

演讲文字

对于Speech to text,您可以采取其他一些示例...或启用离线模式。

setOnTouchListener用于您想要的点击的方法采用右侧绘制

    import android.app.Activity;
    import android.content.ActivityNotFoundException;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.content.pm.ResolveInfo;
    import android.os.Bundle;
    import android.speech.RecognizerIntent;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Locale;


public class demon extends Activity {


        EditText editComment;
    private final int SPEECH_RECOGNITION_CODE = 1;

        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.demo);
             editComment=(EditText) findViewById(R.id.edittext);


            PackageManager pm = getPackageManager();
            List<ResolveInfo> activities = pm.queryIntentActivities(
                    new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
            if (activities.size() == 0)
            {
                editComment.setEnabled(false);
                editComment.setText("Recognizer not present");
            }
            editComment.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    final int DRAWABLE_LEFT = 0;
                    final int DRAWABLE_TOP = 1;
                    final int DRAWABLE_RIGHT = 2;
                    final int DRAWABLE_BOTTOM = 3;

                    if(event.getAction() == MotionEvent.ACTION_UP) {
                        if(event.getRawX() >= (editComment.getRight() - editComment.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                            // your action here

                            startSpeechToText();
                            return true;
                        }
                    }
                    return false;
                }
            });


        }


    private void startSpeechToText() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                "Speak something...");
        try {
            startActivityForResult(intent, SPEECH_RECOGNITION_CODE);
        } catch (ActivityNotFoundException a) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! Speech recognition is not supported in this device.",
                    Toast.LENGTH_SHORT).show();
        }
    }
    /**
     * Handle the results from the voice recognition activity.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case SPEECH_RECOGNITION_CODE: {
                if (resultCode == RESULT_OK && null != data) {
                    ArrayList<String> result = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    String text = result.get(0);
                    editComment.setText(text);
                }
                break;
            }
        }
    }
    }
  

发送短信

<强> demo.java

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Locale;



public class demo extends Activity {


    EditText editComment;
    TextToSpeech t1;
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.demo);
         editComment=(EditText) findViewById(R.id.edittext);
        editComment.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int DRAWABLE_LEFT = 0;
                final int DRAWABLE_TOP = 1;
                final int DRAWABLE_RIGHT = 2;
                final int DRAWABLE_BOTTOM = 3;

                if(event.getAction() == MotionEvent.ACTION_UP) {
                    if(event.getRawX() >= (editComment.getRight() - editComment.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                        // your action here

                        String toSpeak = editComment.getText().toString();
                        Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show();
                        t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
                        return true;
                    }
                }
                return false;
            }
        });
        t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(status != TextToSpeech.ERROR) {
                    t1.setLanguage(Locale.UK);
                }
            }
        });

    }

    public void onPause(){
        if(t1 !=null){
            t1.stop();
            t1.shutdown();
        }
        super.onPause();
    }
}

<强> demo.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">

 <EditText
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/edittext"
  android:padding="10dp"
  android:layout_margin="10dp"
  android:drawableRight="@drawable/icon"/>

</RelativeLayout>

启用离线模式

http://www.androidhive.info/wp-content/uploads/2014/07/1.png