Android - 方法中特定点的用户输入
Android - User input at specific point in method
我正在编写一个飞镖计数器应用程序,目前我正在尝试通过按下特定按钮来获取用户输入(他们击中了飞镖板上的哪个字段)。
每次按下按钮都会 return 一个整数,它将用于更新我的适配器使用的列表值,然后更新视图。
应该发生的方法如下所示:
private void startRound(MatchActivityPlayerAdapter adapter) {
for (int playerIndex = 0; playerIndex < getMatchParticipants().size(); playerIndex++) {
for (int currentDart = 1; currentDart <= maximumDartsToThrow; currentDart++) {
// Here I want the activity to "wait" until the user presses a button
if (pointsButtonClicked) {
setDartValue(playerIndex, currentDart);
setDartsCombinedValues(playerIndex, currentDart);
setRemainingPointsValues(playerIndex, currentDart);
adapter.notifyDataSetChanged();
}
}
}
setCurrentRound(getCurrentRound() + 1);
}
因为在用户进行输入之前,我无法在上述点真正停止 activity,所以我可能必须包含一个 while 循环。但是为了做到这一点,我认为我必须创建第二个线程并以不同的方式处理事情。这实际上让我不知所措,尽管我一直在阅读 this.
任何人都可以向我解释我必须如何设计我的代码才能实现我想要的吗?
编辑:实际上 pointsButtonClicked
是一个布尔值。
我在全局 OnClick 方法中有 20 多个按钮,只要单击其中一个按钮,pointsButtonClicked 就会设置为 true。
Button button1 = findViewById(R.id.btn1);
button1.setOnClickListener(v -> {
outputInt = 1;
pointsButtonClicked = true;
});
Button button2 = findViewById(R.id.btn2);
button2.setOnClickListener(v -> {
outputInt = 2;
pointsButtonClicked = true;
});
// [...]
我认为没有 while-wait 循环应该有更好的方法。
首先,我假设您使用 android.widget.Button
来实现 pointsButtonClicked
我会这样做:
//Global in your activity
int playerIndex = 0;
int currentDart = 1;
然后
private void startRound(MatchActivityPlayerAdapter adapter)
{
pointsButtonClicked.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
setDartValue(playerIndex, currentDart);
setDartsCombinedValues(playerIndex, currentDart);
setRemainingPointsValues(playerIndex, currentDart);
adapter.notifyDataSetChanged();
nextThrow();
}
});
}
最后
//Also in your activity
private void nextThrow()
{
if (currentDart == maximumDartsToThrow)
{
currentDart = 1;
if (playerIndex == getMatchParticipants().size()-1)
{
playerIndex = 0;
setCurrentRound(getCurrentRound() + 1);
}
else
{
++playerIndex;
}
}
else
{
++currentDart;
}
}
可以更好
您可以使用私有变量并通过 getter 和 setter
访问它们
解释
使用这种方法,您无需创建等待按下每个按钮的线程。您只需创建一个按钮来监听它接收到的每个点击事件,然后应用逻辑为每个玩家循环播放器和飞镖。
希望对您有所帮助。
[编辑]
在这种情况下,我会对您使用的每个按钮应用相同的 OnClickListener。
首先,我会创建一个内部 class 来实现 OnClickListener 并允许您管理 outputInt 变量。
//Also in Activity (it is an inner class)
public class MyOnClickListener implements View.OnClickListener
{
private int myOutputInt;
public MyOnClickListener (int outputInt)
{
this.myOutputInt = outputInt;
}
@Override
public void onClick(View v)
{
pointsButtonClicked = true;
outputInt = myOutputInt;
setDartValue(playerIndex, currentDart);
setDartsCombinedValues(playerIndex, currentDart);
setRemainingPointsValues(playerIndex, currentDart);
nextThrow();
}
}
然后创建 20 个 MyOnClickListener,只传递要分配的 int。例如:
button1.setOnClickListener(new MyOnClickListener(1));
button2.setOnClickListener(new MyOnClickListener(2));
button3.setOnClickListener(new MyOnClickListener(3));
etc etc...
我正在编写一个飞镖计数器应用程序,目前我正在尝试通过按下特定按钮来获取用户输入(他们击中了飞镖板上的哪个字段)。 每次按下按钮都会 return 一个整数,它将用于更新我的适配器使用的列表值,然后更新视图。
应该发生的方法如下所示:
private void startRound(MatchActivityPlayerAdapter adapter) {
for (int playerIndex = 0; playerIndex < getMatchParticipants().size(); playerIndex++) {
for (int currentDart = 1; currentDart <= maximumDartsToThrow; currentDart++) {
// Here I want the activity to "wait" until the user presses a button
if (pointsButtonClicked) {
setDartValue(playerIndex, currentDart);
setDartsCombinedValues(playerIndex, currentDart);
setRemainingPointsValues(playerIndex, currentDart);
adapter.notifyDataSetChanged();
}
}
}
setCurrentRound(getCurrentRound() + 1);
}
因为在用户进行输入之前,我无法在上述点真正停止 activity,所以我可能必须包含一个 while 循环。但是为了做到这一点,我认为我必须创建第二个线程并以不同的方式处理事情。这实际上让我不知所措,尽管我一直在阅读 this.
任何人都可以向我解释我必须如何设计我的代码才能实现我想要的吗?
编辑:实际上 pointsButtonClicked
是一个布尔值。
我在全局 OnClick 方法中有 20 多个按钮,只要单击其中一个按钮,pointsButtonClicked 就会设置为 true。
Button button1 = findViewById(R.id.btn1);
button1.setOnClickListener(v -> {
outputInt = 1;
pointsButtonClicked = true;
});
Button button2 = findViewById(R.id.btn2);
button2.setOnClickListener(v -> {
outputInt = 2;
pointsButtonClicked = true;
});
// [...]
我认为没有 while-wait 循环应该有更好的方法。
首先,我假设您使用 android.widget.Button
来实现 pointsButtonClicked
我会这样做:
//Global in your activity
int playerIndex = 0;
int currentDart = 1;
然后
private void startRound(MatchActivityPlayerAdapter adapter)
{
pointsButtonClicked.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
setDartValue(playerIndex, currentDart);
setDartsCombinedValues(playerIndex, currentDart);
setRemainingPointsValues(playerIndex, currentDart);
adapter.notifyDataSetChanged();
nextThrow();
}
});
}
最后
//Also in your activity
private void nextThrow()
{
if (currentDart == maximumDartsToThrow)
{
currentDart = 1;
if (playerIndex == getMatchParticipants().size()-1)
{
playerIndex = 0;
setCurrentRound(getCurrentRound() + 1);
}
else
{
++playerIndex;
}
}
else
{
++currentDart;
}
}
可以更好
您可以使用私有变量并通过 getter 和 setter
访问它们解释
使用这种方法,您无需创建等待按下每个按钮的线程。您只需创建一个按钮来监听它接收到的每个点击事件,然后应用逻辑为每个玩家循环播放器和飞镖。
希望对您有所帮助。
[编辑]
在这种情况下,我会对您使用的每个按钮应用相同的 OnClickListener。
首先,我会创建一个内部 class 来实现 OnClickListener 并允许您管理 outputInt 变量。
//Also in Activity (it is an inner class)
public class MyOnClickListener implements View.OnClickListener
{
private int myOutputInt;
public MyOnClickListener (int outputInt)
{
this.myOutputInt = outputInt;
}
@Override
public void onClick(View v)
{
pointsButtonClicked = true;
outputInt = myOutputInt;
setDartValue(playerIndex, currentDart);
setDartsCombinedValues(playerIndex, currentDart);
setRemainingPointsValues(playerIndex, currentDart);
nextThrow();
}
}
然后创建 20 个 MyOnClickListener,只传递要分配的 int。例如:
button1.setOnClickListener(new MyOnClickListener(1));
button2.setOnClickListener(new MyOnClickListener(2));
button3.setOnClickListener(new MyOnClickListener(3));
etc etc...