为ListView设置OnclickListener这样在列表中选择的行将用作另一个类中的编辑文本

时间:2014-04-17 21:23:37

标签: java android eclipse listview

我的翻译应用程序有两个课程。我们讲的一个(Voice.java)和RecognizerIntent获取我们的演讲并以列表的形式显示它。这是voice.java

package com.example.testing;

import java.util.ArrayList;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class Voice extends Activity implements OnClickListener{

    ListView lv;
    static final int check=1111;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.voice);
        lv=(ListView)findViewById(R.id.lvVoiceReturn);
        Button b=(Button)findViewById(R.id.bVoice);
        b.setOnClickListener(this);
        lv.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {

        // TODO Auto-generated method stub
        switch(arg0.getId()){
        case R.id.bVoice:
        Intent i=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak Up");
        startActivityForResult(i, check);
        break;




    }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if(requestCode == check && resultCode==RESULT_OK){

            ArrayList<String> results=data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
            }

        super.onActivityResult(requestCode, resultCode, data);
    }


}

另一个类TranslateActivity,它有一个按钮和两个编辑文本。按钮用作文本的翻译器,从第一个编辑文本到第二个编辑文本中的翻译文本,并且还说出翻译的文本。代码如下:

package com.example.testing;

    import java.util.Locale;

    import android.app.Activity;
    import android.os.Bundle;
    import android.speech.tts.TextToSpeech;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;

    import com.memetix.mst.language.Language;
    import com.memetix.mst.translate.Translate;

    public class TranslateActivity extends Activity implements OnClickListener {
        EditText etInput

;
    EditText etOutput;
    TextToSpeech tts;
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_translate);
        Translate.setClientId("d6159b31-37ba-4668-a31f-de0de6c47a38");
        Translate.setClientSecret("UMjSB3cHaSkLE+2xw4+a4dLIFahLdQQqh1YOhXdqfh4");
        Button Trans = (Button)findViewById(R.id.translate);
        etInput = (EditText)findViewById(R.id.input);
        etOutput = (EditText)findViewById(R.id.output);
        Trans.setOnClickListener(this);
        tts =new TextToSpeech(TranslateActivity.this, new TextToSpeech.OnInitListener() {

            @Override
            public void onInit(int status) {
                // TODO Auto-generated method stub
                if(status !=TextToSpeech.ERROR){
                    tts.setLanguage(Locale.US);
                }
            }
        });

    }

    @Override
    public void onClick(View v) {   
        //get the text entered      
                String In =etInput.getText().toString();
                String s[]=In.split(" ");
                //String out[]=  new String[3];
                String str=new String();
                String outString=new String();
                //String Out;
                try {
                    for(int i=0;i<s.length;i++){
                         if(s[i].equals("who")){
                             str="whoa";
                            //etOutput.setText("whoa");
                            //str=etOutput.getText().toString();
                            //tts.speak(str,TextToSpeech.QUEUE_FLUSH, null);    
                        }else if(s[i].equals("are")){
                          //    etOutput.setText("arey");
                              str="arey";
                            //str=etOutput.getText().toString();
                            //tts.speak(str,TextToSpeech.QUEUE_FLUSH, null);
                        }else if(s[i].equals("you")){
                         str="ram";

                            //etOutput.setText("your");
                            //str=etOutput.getText().toString();
                            //tts.speak(str,TextToSpeech.QUEUE_FLUSH, null);
                        }   
                         outString+=str ;
                         outString+=" ";

                    }
                    tts.speak(outString,TextToSpeech.QUEUE_FLUSH, null);
                  //  String Out = Translate.execute(In, Language.AUTO_DETECT, Language.FRENCH);

               //   etInput.setText(Out);
        //        etOutput.setText(Out);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
}
}

现在我想要一个用于列表视图的单击侦听器,这样无论我在列表View上单击哪一行,它都将被用作第二类(TranslateActivity.java)的编辑文本并在不单击第二类上的按钮的情况下进行翻译。这是第二类按钮的功能,只需单击第一类中的列表视图即可。任何知道如何操作的人都会这样做?

0 个答案:

没有答案