我正在创建一个简单的应用程序,试图学习功能和实现基本功能。我的主要目标是让语音命令在我打开应用程序的地方工作,只需说转到下一页,然后它就会这样做等等。有没有办法使用 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);
}
最佳答案
你可以这样做
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 毫秒,即 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:如果回答有帮助请采纳
关于android - 如何自动将 ListView 字段的第一行传递给 EditText 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36995116/