如何自动将ListView字段的第一行传递给EditText字段

时间:2016-05-03 03:30:16

标签: android listview android-studio android-intent android-edittext

我正在创建一个简单的应用程序,试图学习功能和实现基本功能。我的主要目标是在打开应用程序的地方使用语音命令,然后简单地说转到下一页然后它就这样做等等。有没有办法使用android studio这样做,因为我没有遇到过任何例子。

我设法让语音识别工作,它为ListView中的文本语句提供语音。我正在尝试使用登录页面创建一个简单的应用程序,用户可以在其中键入正确的凭据并继续下一页或使用语音文本填写凭据,如果正确则自动转到下一页。

我的问题是,是否有办法自动将ListView中的第一行语句传入用户名字段,该字段当前是EditText字段,然后在1-2秒后自动按下登录按钮。我已经附加了主java文件来执行此操作,并且非常感谢代码的一些帮助。请注意,目前我只是使用用户名进行验证,密码字段并不重要。

ListView lv;
static final int check = 1111;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView)findViewById(R.id.lvVoiceReturn);
    Button b = (Button)findViewById(R.id.bVoice);
    b.setOnClickListener(this);///////////////////
}

public void onButtonClick(View v)
{
    if (v.getId() == R.id.Blogin)
    {
        EditText a = (EditText)findViewById(R.id.TFusername);
        String str = a.getText().toString();


        //Go to the next 'Display' window or activity if the person enters the correct username which is not case sensitive
        if (str.equalsIgnoreCase("next"))
        {
            Intent userintent = new Intent(MainActivity.this, Display.class);
            startActivity(userintent);
        }
        else
        {
            Toast.makeText(getApplicationContext(), "Incorrect Information", Toast.LENGTH_SHORT).show();
        }
    }
}

@Override
public void onClick(View v) {
    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Repeat Again");
    startActivityForResult(i, check);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    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));
        //??String selectedFromList = (lv.getItemAtPosition(position))////////
    }


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

}

1 个答案:

答案 0 :(得分:1)

你可以这样做

a.setText((String)lv.getItemAtPosition(0));

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            b.performClick();
                        }
                    }, 2000);

第一行从listView的第一行设置editText,然后handler执行事件2000 ms,即2秒后。

完整代码:

ListView lv;
static final int check = 1111;
Button b;
EditText a;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView)findViewById(R.id.lvVoiceReturn);
    b = (Button)findViewById(R.id.bVoice);
    a = (EditText)findViewById(R.id.TFusername);

    b.setOnClickListener(this);///////////////////
}

public void onButtonClick(View v)
{
    if (v.getId() == R.id.Blogin)
    {
        String str = a.getText().toString();


        //Go to the next 'Display' window or activity if the person enters the correct username which is not case sensitive
        if (str.equalsIgnoreCase("next"))
        {
            Intent userintent = new Intent(MainActivity.this, Display.class);
            startActivity(userintent);
        }
        else
        {
            Toast.makeText(getApplicationContext(), "Incorrect Information", Toast.LENGTH_SHORT).show();
        }
    }
}

@Override
public void onClick(View v) {
    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Please Repeat Again");
    startActivityForResult(i, check);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    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));
        //??String selectedFromList = (lv.getItemAtPosition(position))////////

        a.setText((String)lv.getItemAtPosition(0));

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            b.performClick();
                        }
                    }, 2000);
    }


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

PS:如果答案有帮助那么请接受。