Java 单击按钮1,然后单击按钮2,将生成一个;“字母或字符”安卓
如果标题仍然有点混乱,我真正的意思是“当我点击按钮1,然后点击按钮2(有一点时间间隔),它将生成一个“字母”或“字符”,这取决于你在上面编码的值,它将在文本框中输入 请帮助我完成我的android应用程序需要这个。我很难处理这个 我的代码:Java 单击按钮1,然后单击按钮2,将生成一个;“字母或字符”安卓,java,android,arrays,switch-statement,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" and
<Button
android:id="@+id/block1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:textSize="16sp" />
然后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..
请告诉我我缺少什么或需要修改什么。如果我错了,请纠正我。这已经是一个工作程序,但正如我前面所说的,我需要一个**“一个接一个单击”,然后它将在文本框中生成一个“变量/字符”
编辑:
我将根据我对你所问问题的理解写一个简单的答案 我建议您添加一个变量
boolean aPressed = false;
然后在click listener for A中,将变量更改为true,启动计时器,然后在B按钮的click listener中使用if语句测试是否从变量A中单击了A。然后停止计时器,查看所用时间是否符合您的要求,并按照您的意愿将文本放入文本框中
我将不做更详细的说明,因为不清楚你在问什么,我很惊讶这个问题还没有结束
希望我能帮忙,
-Daniel以下是简单的逻辑,而不是可以工作的精确代码
// 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 = "";
}
}
我们感谢您的努力,但您目前面临的问题是什么?您需要具体说明issue@PavneetSingh你好,你好,我添加了一张图片:)只是我不知道如何实现“例如:当你点击第一块(buttonA),然后点击第二块(buttonB),它会生成一个“文本框上的值”先生,您需要使用
findViewById
初始化textview
并使用textview.setText(字母)代码>内部单击Listener我已经得到了:唯一剩下的是按钮的组合。Block1+Block2(一个接一个,每次点击都有一个时间间隔)它将生成一个“字符或变量”。。你能帮我拿一下吗(你是想总是在点击两个按钮后触发“动作”还是在可能更长的组合后触发“动作”?您好,我的真正意思是“点击两个按钮后它将触发动作”中间或一起有一个句点?我不明白你的意思,但例如:当你点击按钮a,然后点击按钮B,它将触发一个动作。(取决于你输入的代码)你好,因为我必须从“a-Z”生成一个字母所以我认为你的方法行不通。这就是我的目标。在检查了你想要做什么之后,我认为6个按钮(即值为1,2,4,8,16,32)的位/标志系统会更好。例如:1=a,(1+4)=B,(1+2)=C…等等。唯一的问题是如何“注册”该字母组合是否完整或有效?无论哪种方式,我建议您删除此问题,并创建一个更清晰的新问题,包括更多关于您真正想做什么(以及您尝试过什么)的详细信息,因为您当前的问题不符合您的目标。例如,单击两个按钮,然后单击“操作”,但有时单击1(a)有时是5(Q)。
// 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 = "";
}
}