我创建了几个按钮,当用户单击第一个按钮时,然后将第一个按钮的边框更改为red
,然后只有用户可以单击第二个按钮上的按钮。
该程序可以记录用户按下的第二个按钮并显示 到控制台。
稍后,我想将第一个按钮的背景更改为第二个按钮的背景。
box = new ResizeButton[]{
(ResizeButton) findViewById(R.id.kotak1),
(ResizeButton) findViewById(R.id.kotak2),
(ResizeButton) findViewById(R.id.kotak3),
(ResizeButton) findViewById(R.id.kotak4),
(ResizeButton) findViewById(R.id.kotak5),
(ResizeButton) findViewById(R.id.kotak6),
(ResizeButton) findViewById(R.id.kotak7),
(ResizeButton) findViewById(R.id.kotak8),
(ResizeButton) findViewById(R.id.kotak9)
};
boxPlay = new int[]{0,0,0,0,0,0,0,0,0}; // This is to identify player 1 or player 2 win
box[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
box[currentButton].setImageDrawable(getResources().getDrawable(R.drawable.border_red));
int changeWith;
do {
changeWith = selectedButton();
}while (changeWith == -1);
Log.d("TAG", "Change: " + changeWith);
}
private int selectedButton() {
returnCode = -1;
for(int i = 0; i < box.length;i++){
final int selected = i;
box[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(boxPlay[selected] == 0)
returnCode = selected;
else
invalidSelect();
}
});
}
return returnCode;
}
}
运行此代码时出现错误:
原因:输入分派超时(正在等待,因为触摸 窗口尚未完成对输入事件的处理 之前已交付给它。) 负载:1.68 / 1.09 / 0.72
如何解决这个问题?非常感谢您的时间和协助。
答案 0 :(得分:1)
只需在每次单击按钮时在“所有”按钮中设置默认颜色即可。 然后在可点击按钮中设置边框颜色。像下面一样
box[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setDefualtColor()
box[currentButton].setImageDrawable(getResources().getDrawable(R.drawable.border_red));
int changeWith;
do {
changeWith = selectedButton();
}while (changeWith == -1);
Log.d("TAG", "Change: " + changeWith);
}
private void setDefualtColor(){
for(int i = 0; i < box.length;i++){
box[i].setBackground(ContextCompat.getDrawable(baseActivity, R.drawable.defualt_color));
}
}
答案 1 :(得分:0)
根据您的代码,如果最后一个按钮!= 0,它将返回invalidSelect()
,因此在boxPlay[selected] == 0
时返回值。
private int selectedButton() {
returnCode = -1;
for(int i = 0; i < box.length;i++){
final int selected = i;
box[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(boxPlay[selected] == 0)
return selected;
else
invalidSelect();
}
});
}
return returnCode;
希望这行得通
答案 2 :(得分:0)
this是添加句柄边框的好方法。
对于第二部分,将按钮视图的实例保存在变量中,然后在单击其他按钮时访问它。
private int[] boxPlay = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0}; // This is to identify player 1 or player 2 win
private Button lastSelectedbutton = null //to store the current selected button instance to get the drawable and reset the button
box[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v){
//this v is the current selected button view's instance
// check here if button this is the second button or first
if (lastSelectedbutton != null) {
//means this is second button
setUpSecondButton(v, lastSelectedButton);
lastSelectedButton = null;
} else {
box[currentButton].setImageDrawable(getResources()
.getDrawable(R.drawable.border_red));
lastSelectedButton = (Button) v;
int changeWith;
do {
changeWith = selectedButton();
} while (changeWith == -1);
Log.d("TAG", "Change: " + changeWith);
}
}
private void setUpSecondButton (Button secondButton, Button firstButton){
//copy the drawable to second button and reset first button as per need.
secondButton.setBackground(firstButton.getBackground())
firstButton.setBackground(null)
}
private int selectedButton () {
returnCode = -1;
for (int i = 0; i < box.length; i++) {
final int selected = i;
box[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (boxPlay[selected] == 0)
returnCode = selected;
else
invalidSelect();
}
});
}
return returnCode;
}
}
答案 3 :(得分:-1)
Xml:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="btn1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="btn2"/>
Java:
public void btn1(View v){
//perform any task on btn1 click
btn2(v);//btn2 click on 1st button click
}
public void btn2(View v){
//perform any task on btn2 click
}
或者您可以通过按钮ID进行点击
box[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
box[currentButton].setImageDrawable(getResources().getDrawable(R.drawable.border_red));
int changeWith;
do {
changeWith = selectedButton();
}while (changeWith == -1);
Log.d("TAG", "Change: " + changeWith);
box[i+1].performClick();
box[6].performClick();//or
}