单击Button1然后单击Button2将生成一个“字母或字符” - Android

时间:2016-12-31 14:49:49

标签: java android arrays switch-statement

如果标题仍然有点令人困惑,我的意思是“当我点击按钮1后跟按钮2(有一个时间间隔)时,它将生成一个”字母“或”字符“,具体取决于你的值在其上编码,它将在文本框中输入。

请帮助我需要这个来完成我的Android应用程序。我很难处理这个问题。

我的代码:

<Button
android:id="@+id/block1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:textSize="16sp" />

在我的onCreate()

Button A = (Button) findViewById(R.id.block1);
Button B = (Button) findViewById(R.id.block2);

然后是setOnClickListener

A.setOnClickListener(this);
B.setOnClickListener(this);

在我的onClick()方法中,我确实声明了一个全局变量。 String letter;

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.block1:
             letter += "A";  
             tts.speak("A.", TextToSpeech.QUEUE_FLUSH, null);
             break;
        case R.id.block2:
            letter += "B";  
             tts.speak("B.", TextToSpeech.QUEUE_FLUSH, null);
             break;
        .
        .
        just like that..

请告诉我我缺少什么或需要修改什么。请纠正我如果我错了.. 这已经是一个有效的程序,但正如我之前所说,我想要“一次又一次点击”然后它将生成一个“变量” /字符“在文本框中。

编辑:

enter image description here

2 个答案:

答案 0 :(得分:0)

我会根据我对你所问的内容的理解来写一个简单的答案......

我建议你添加一个变量,

boolean aPressed = false;

然后在A的单击侦听器中将变量更改为true,启动计时器,然后在B按钮的单击侦听器测试中使用if语句,如果从变量aPressed中单击了A。然后停止计时器,查看所用时间是否符合您的要求,并将文本放在文本框内。

我不会详细说明,因为我不清楚你在问什么,我很惊讶这还没有被关闭。

希望我帮助,

<强> -Daniel

答案 1 :(得分:0)

以下是简单的逻辑,而不是确切的代码。

// Replace "String" with the correct type to suit the task
private String mButOneValue = "";

private void twoClickAction(String valueOne, String valueTwo) {
    ... perform the action here using the passed fields ...    
}

// This would be your current switch statement maybe. This 
// prevents a long method with possible code duplication.
private String getValueOfClick(View v) {
    ... extract the "value" you want to track ...
    return theValueYouWorkedOut;
}

@Override
public void onClick(View v) {
    if (mButOneValue.equals("")) {
        // capture the first value you want to track.
        mButOneValue = getValueOfClick(v);

    } else {
         // first click value is known, get the second and
         // and perform the action you want.
         twoClickAction(mButOneValue, getValueOfClick(v));

         // And be sure to clear the value first value.
         mButOneValue = "";
    }
}