有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java按钮1,然后单击按钮2将生成一个“字母或字符”Android

如果标题仍然有点混乱,我真正的意思是“当我点击按钮1,然后点击按钮2(有一点时间间隔),它将生成一个“字母”或“字符”,这取决于你在上面编码的值,它将在文本框中输入

请帮助我完成我的安卓应用程序。我很难处理这个问题

我的代码:

<Button
安卓:id="@+id/block1"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="A"
安卓: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) 个答案

  1. # 1 楼答案

    我会根据我对你所问问题的理解写一个简单的答案

    我建议你加一个变量

    boolean aPressed = false;
    

    然后在click listener中,将变量更改为true,启动计时器,然后在B按钮的click listener中使用if语句,测试是否从变量A单击了A。然后停止计时器,看看所用的时间是否符合您的要求,并根据您的意愿将文本放入文本框中

    由于不清楚你在问什么,我将不进行更详细的讨论,我很惊讶这件事还没有结束

    希望我能帮忙,

    -Daniel

  2. # 2 楼答案

    以下是简单的逻辑,而不是可以工作的精确代码

    // 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 = "";
        }
    }