问题描述
所以我有一个按钮在我的应用程序和一个edittext。当我点击按钮并在edittext中写一些东西时,textview会改变。除了一件事以外,它一切都会起作用的。我必须点击按钮两次才能使其工作(只有我第一次打开活动)。在我打开活动之后,我第一次按下按钮,没有任何反应,之后它的工作原理。
我已经做了我的研究,直到我知道导致麻烦的事情是焦点,但是我尝试了一些东西,没有任何工作。
按钮XML代码:
< Button
android:id =@ + id / submitButton
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_alignBaseline =@ + id / editText1
android:layout_alignBottom =@ + id / editText1
android:layout_alignLeft =@ + id / checkBox25
android:text =@ string / addMaterial
android:onClick =submitQuantityButton>
< / Button>
编辑文本XML代码:
< EditText
android:id =@ + id / editText1
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_alignParentLeft =true
android:layout_below =@ + id / spinner1
android:ems =3
android:inputType =number
android :maxLength =3>
< / EditText>
我尝试添加android:focusableInTouchMode =false到按钮XML,我也尝试添加requestFocus到按钮XML,它仍然不起作用。我也从edittext中删除了requestFocus,它不起作用。我没有想法还有什么尝试。
onClick方法:
public void submitQuantityButton(View v){
Button submitButton =(Button)findViewById(R.id.submitButton);
final Spinner sItems =(Spinner)findViewById(R.id.spinner1);
final Context context = this;
final CheckBox cb4 =(CheckBox)findViewById(R.id.checkBox4);
final CheckBox cb5 =(CheckBox)findViewById(R.id.checkBox5);
final CheckBox cb33 =(CheckBox)findViewById(R.id.checkBox33);
final CheckBox cb30 =(CheckBox)findViewById(R.id.checkBox30);
final CheckBox cb6 =(CheckBox)findViewById(R.id.checkBox6);
final CheckBox cb7 =(CheckBox)findViewById(R.id.checkBox7);
final CheckBox cb9 =(CheckBox)findViewById(R.id.checkBox9);
final CheckBox cb10 =(CheckBox)findViewById(R.id.checkBox10);
final CheckBox cb11 =(CheckBox)findViewById(R.id.checkBox11);
final CheckBox cb12 =(CheckBox)findViewById(R.id.checkBox12);
//
final AlertDialog.Builder emptyETextErrorBuilder = new AlertDialog.Builder(context);
emptyETextErrorBuilder.setTitle(警告);
emptyETextErrorBuilder.setMessage(请在按此按钮之前输入一个值);
emptyETextErrorBuilder.setPositiveButton(OK,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
dialog cancel();
}
});
submitButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
final int position = sItems .getSelectedItemPosition();
EditText quantityEditText =(EditText)findViewById(R.id.editText1);
switch(position){
case 0:
AlertDialog。 Builder spinnerErrorBuilder = new AlertDialog.Builder(context);
spinnerErrorBuilder.setTitle(Warning);
spinnerErrorBuilder.setMessage(请从上面的列表中选择一个项,然后输入一个值);
spinnerErrorBuilder.setPositiveButton(OK,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int id){
dialog cancel();
}
});
AlertDialog spinnerError = spinnerErrorBuilder.create();
spinnerError.show();
break;
case 1:
String item1 = quantityEditText.getText()。toString();
if(item1.matches())
{
AlertDialog emptyETextError = emptyETextErrorBuilder.create();
emptyETextError.show();
}
else
{
cb4.setText(精加工图腾(+ item1 +/ 250));
}
break;
case 2:
String item2 = quantityEditText.getText()。toString();
if(item2.matches())
{
AlertDialog emptyETextError = emptyETextErrorBuilder.create();
emptyETextError.show();
}
else
{
cb5.setText(Pile of Crystalline Dust(+ item2 +/ 250));
}
break;
case 3:
String item3 = quantityEditText.getText()。toString();
if(item3.matches())
{
AlertDialog emptyETextError = emptyETextErrorBuilder.create();
emptyETextError.show();
}
else
{
cb33.setText(Pile of Crystalline Dust(+ item3 +/ 250));
}
break;
case 4:
String item4 = quantityEditText.getText()。toString();
if(item4.matches())
{
AlertDialog emptyETextError = emptyETextErrorBuilder.create();
emptyETextError.show();
}
else
{
cb30.setText(Pile of Crystalline Dust(+ item4 +/ 250));
}
break;
case 5:
String item5 = quantityEditText.getText()。toString();
if(item5.matches())
{
AlertDialog emptyETextError = emptyETextErrorBuilder.create();
emptyETextError.show();
}
else
{
cb6.setText(强力毒液囊(+ item5 +/ 250));
}
break;
case 6:
String item6 = quantityEditText.getText()。toString();
if(item6.matches())
{
AlertDialog emptyETextError = emptyETextErrorBuilder.create();
emptyETextError.show();
}
else
{
cb7.setText(Vial of Powerful Blood(+ item6 +/ 250));
}
break;
case 7:
String item7 = quantityEditText.getText()。toString();
if(item7.matches())
{
AlertDialog emptyETextError = emptyETextErrorBuilder.create();
emptyETextError.show();
}
else
{
cb9.setText(Ancient Bone(+ item7 +/ 250));
}
break;
case 8:
String item8 = quantityEditText.getText()。toString();
if(item8.matches())
{
AlertDialog emptyETextError = emptyETextErrorBuilder.create();
emptyETextError.show();
}
else
{
cb10.setText(装甲量表(+ item8 +/ 250));
}
break;
case 9:
String item9 = quantityEditText.getText()。toString();
if(item9.matches())
{
AlertDialog emptyETextError = emptyETextErrorBuilder.create();
emptyETextError.show();
}
else
{
cb11.setText(Vicious Claw(+ item9 +/ 250));
}
break;
case 10:
String item10 = quantityEditText.getText()。toString();
if(item10.matches())
{
AlertDialog emptyETextError = emptyETextErrorBuilder.create();
emptyETextError.show();
}
else
{
cb12.setText(Vicious Fang(+ item10 +/ 250));
}
break;
}
}
});
}
好吧,我终于弄清楚了是什么造成的问题,由我自己。我不敢相信我错过了这样一个明显的问题。引起问题的事情不是重点,而是方法本身。在我的XML文件中,我通过android调用onClick方法:onClick =onClick,然后我还在onClick方法中添加了一个buttonlistener到java代码。
我所做的只是删除buttonlistener,没有更多的双击必要!所以如果有人有这个问题,请务必确保你没有同时使用onClick方法AND按钮列表。
错误的代码:
public void submitQuantityButton(View v){
/ pre>
submitButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
。
。
。// REST OF THE CODE
为了使其工作,我只需删除onclick侦听器,只留下:
code> public void submitQuantityButton(View v){
。
。
。// REST OF THE CODE
So I have a button in my app and an edittext. When I click the button and write something in the edittext, a textview changes. It all works as it should except for one thing. I have to click on the button twice to make it work (only the first time I open activity). The very first time after I open activity I press the button and nothing happens, after that it works as it should.
I already did my research on this and as far as I know the thing that is causing trouble is focus, but I tried a few things and nothing worked.
Button XML code:
<Button android:id="@+id/submitButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/editText1" android:layout_alignBottom="@+id/editText1" android:layout_alignLeft="@+id/checkBox25" android:text="@string/addMaterial" android:onClick="submitQuantityButton" > </Button>
Edittext XML code:
<EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/spinner1" android:ems="3" android:inputType="number" android:maxLength="3" > </EditText>
I tried adding android:focusableInTouchMode="false" to button XML, I also tried adding requestFocus to button XML and it still doesn't work. I also removed the requestFocus from edittext and it doesn't work. I'm running out of ideas what else to try.
onClick method:
public void submitQuantityButton (View v){ Button submitButton = (Button)findViewById(R.id.submitButton); final Spinner sItems = (Spinner)findViewById(R.id.spinner1); final Context context = this; final CheckBox cb4 = (CheckBox) findViewById(R.id.checkBox4); final CheckBox cb5 = (CheckBox) findViewById(R.id.checkBox5); final CheckBox cb33 = (CheckBox) findViewById(R.id.checkBox33); final CheckBox cb30 = (CheckBox) findViewById(R.id.checkBox30); final CheckBox cb6 = (CheckBox) findViewById(R.id.checkBox6); final CheckBox cb7 = (CheckBox) findViewById(R.id.checkBox7); final CheckBox cb9 = (CheckBox) findViewById(R.id.checkBox9); final CheckBox cb10 = (CheckBox) findViewById(R.id.checkBox10); final CheckBox cb11 = (CheckBox) findViewById(R.id.checkBox11); final CheckBox cb12 = (CheckBox) findViewById(R.id.checkBox12); // final AlertDialog.Builder emptyETextErrorBuilder = new AlertDialog.Builder(context); emptyETextErrorBuilder.setTitle("Warning"); emptyETextErrorBuilder.setMessage("Please enter a value before pressing this button"); emptyETextErrorBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); submitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final int position = sItems.getSelectedItemPosition(); EditText quantityEditText = (EditText)findViewById(R.id.editText1); switch (position){ case 0: AlertDialog.Builder spinnerErrorBuilder = new AlertDialog.Builder(context); spinnerErrorBuilder.setTitle("Warning"); spinnerErrorBuilder.setMessage("Please choose an item from the list above and then enter a certain value"); spinnerErrorBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog spinnerError = spinnerErrorBuilder.create(); spinnerError.show(); break; case 1: String item1 = quantityEditText.getText().toString(); if (item1.matches("")) { AlertDialog emptyETextError = emptyETextErrorBuilder.create(); emptyETextError.show(); } else { cb4.setText("Elaborate Totem (" + item1 + "/250)"); } break; case 2: String item2 = quantityEditText.getText().toString(); if (item2.matches("")) { AlertDialog emptyETextError = emptyETextErrorBuilder.create(); emptyETextError.show(); } else { cb5.setText("Pile of Crystalline Dust (" + item2 + "/250)"); } break; case 3: String item3 = quantityEditText.getText().toString(); if (item3.matches("")) { AlertDialog emptyETextError = emptyETextErrorBuilder.create(); emptyETextError.show(); } else { cb33.setText("Pile of Crystalline Dust (" + item3 + "/250)"); } break; case 4: String item4 = quantityEditText.getText().toString(); if (item4.matches("")) { AlertDialog emptyETextError = emptyETextErrorBuilder.create(); emptyETextError.show(); } else { cb30.setText("Pile of Crystalline Dust (" + item4 + "/250)"); } break; case 5: String item5 = quantityEditText.getText().toString(); if (item5.matches("")) { AlertDialog emptyETextError = emptyETextErrorBuilder.create(); emptyETextError.show(); } else { cb6.setText("Powerful Venom Sac (" + item5 + "/250)"); } break; case 6: String item6 = quantityEditText.getText().toString(); if (item6.matches("")) { AlertDialog emptyETextError = emptyETextErrorBuilder.create(); emptyETextError.show(); } else { cb7.setText("Vial of Powerful Blood (" + item6 + "/250)"); } break; case 7: String item7 = quantityEditText.getText().toString(); if (item7.matches("")) { AlertDialog emptyETextError = emptyETextErrorBuilder.create(); emptyETextError.show(); } else { cb9.setText("Ancient Bone (" + item7 + "/250)"); } break; case 8: String item8 = quantityEditText.getText().toString(); if (item8.matches("")) { AlertDialog emptyETextError = emptyETextErrorBuilder.create(); emptyETextError.show(); } else { cb10.setText("Armored Scale (" + item8 + "/250)"); } break; case 9: String item9 = quantityEditText.getText().toString(); if (item9.matches("")) { AlertDialog emptyETextError = emptyETextErrorBuilder.create(); emptyETextError.show(); } else { cb11.setText("Vicious Claw (" + item9 + "/250)"); } break; case 10: String item10 = quantityEditText.getText().toString(); if (item10.matches("")) { AlertDialog emptyETextError = emptyETextErrorBuilder.create(); emptyETextError.show(); } else { cb12.setText("Vicious Fang (" + item10 + "/250)"); } break; } } }); }
解决方案Okay so I finally figured out what caused the problem, by myself. I can't believe I missed such an obvious issue. The thing that caused problem wasn't focus, but the method itself. In my XML file I called onClick method by android:onClick="onClick" and then I also added a buttonlistener inside the onClick method to java code.
All I did was remove the buttonlistener and there's no more double clicking neccessary! So if anyone has this problem in future simply make sure you don't have an onClick method AND buttonlistener at the same time.
Wrong code:
public void submitQuantityButton (View v){ submitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { . . . //REST OF THE CODE
To make it work I simply deleted the onclick listener, leaving only:
public void submitQuantityButton (View v){ . . . //REST OF THE CODE
这篇关于我必须点击按钮两次才能工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!