【发布时间】:2016-05-06 03:02:00
【问题描述】:
有没有一种简单的方法可以在我的 Android 应用程序中打开该窗口/活动后 1 秒内自动按下按钮。
目前我需要按下“bVoice”按钮以使用b.setOnClickListener(this) 开始语音识别。我希望在打开该特定窗口/活动后的 1 秒内自动按下按钮,而不是按下按钮。
有没有一种简单的方法来实现这一点?以下是我的部分代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ListView lv;
static final int check = 1111;
Button b;
EditText a;
Button c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lvVoiceReturn);
a = (EditText) findViewById(R.id.TFusername);
b = (Button) findViewById(R.id.bVoice);
c = (Button)findViewById(R.id.Blogin);
b.setOnClickListener(this);
}
public void onButtonClick(View v) {
if (v.getId() == R.id.Blogin) {
String str = a.getText().toString();
//Go to the relevant page if any part of the phrase or word entered in the 'EditText' field contains 'next' which is not case sensitive
if (str.toLowerCase().contains("next")) {
Intent userintent = new Intent(MainActivity.this, Display_1.class);
startActivity(userintent);
} else if (str.toLowerCase().contains("heart")) {
Intent userintent = new Intent(MainActivity.this, Display_2.class);
startActivity(userintent);
} else {
Toast.makeText(getApplicationContext(), "Incorrect Information", Toast.LENGTH_SHORT).show();
}
}
}
【问题讨论】:
-
为什么要点击按钮??您可以直接在 onCreate() 中检查该条件
-
所以我应该写 b.onCreate 而不是 setonclicklistener?
标签: android android-studio android-intent android-edittext onclicklistener