2013-06-23 200 views
16

所以我在我的應用程序和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> 

EDITTEXT 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> 

我嘗試添加機器人: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("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; 
      } 
     } 
    }); 
} 
+0

當你說幾件事時,你有沒有嘗試清除焦點? – Evilunclebill

+0

是的,我試過,但我忘了在問題中提到它 – Guy

+0

等等,我應該在哪裏嘗試clearfocus?我只嘗試添加它來編輯XML文件中的文本。 – Guy

回答

2

好吧,所以我終於明白自己是什麼導致了這個問題。我不相信我錯過了這樣一個明顯的問題。造成問題的原因並不是焦點,而是方法本身。在我的XML文件中,我通過android:onClick =「onClick」調用了onClick方法,然後我還在onClick方法中添加了一個buttonlistener以用於java代碼。

我所做的只是刪除buttonlistener,沒有更多的雙擊需要!所以如果將來有人有這個問題,只要確保你沒有onClick方法和buttonlistener在同一時間。

錯誤代碼:

public void submitQuantityButton (View v){ 

submitButton.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
. 
. 
. //REST OF THE CODE 

爲了使它工作,我只是刪除的onclick聽衆,只留下:

public void submitQuantityButton (View v){ 
. 
. 
. //REST OF THE CODE 
+0

由於某種原因,這不適合我。我沒有兩個onClicks,我只在代碼中添加一個。它在工作之前確實改變了按鈕的狀態,所以我只需提取onClick方法的內容並在onClick事件和onFocus事件中調用它。就像使用多個按鈕的onClick一樣,使用onFocusChangeListener捕獲所有onFocus事件,然後檢查是否選擇了該按鈕。 –

0

試試這一次
修訂

overrideonResume方法在activity 然後明確的重點從您的EditText和焦點設置到主佈局

​​

button.requestFocus(); 

問題是否與虛擬鍵盤它可以幫助你

AndroidManifest.xml文件的Activity標籤,就把這行裏面

android:windowSoftInputMode="stateHidden" 
+0

感謝您的回答,但它仍然不起作用 – Guy

+0

嘗試'android:descendantFocusability =「beforeDescendants」 android: focusableInTouchMode =「true」'在你的佈局中包含'button'和'edittext' –

+0

在我的相對佈局中添加了這兩個(我認爲這就是你的意思),它可悲的仍然不會工作 – Guy

0

只需使用gettext()來獲取來自EditText的文本 然後使用setText()設置TextView的文本。

23

我的問題是Button XML定義:

android:focusableInTouchMode="true" 

刪除此屬性並且按鈕不需要被觸摸兩次。看起來好像第一次觸摸用於分配按鈕上的焦點,第二次觸摸然後觸發OnClickListener

請注意,按鈕在android:focusable="true"屬性沒有問題。

+0

更改'focusableInTouchMode'是我的解決方案。謝謝! – Merlevede

0

我和OP有同樣的問題。我嘗試了所有與焦點相關的建議,但是他們沒有一個人每次都爲我工作。

我嘗試從我的佈局中刪除NavigationDrawer,但那不起作用。

最後,我試着用LinearLayout替換我的CoordinatorLayout,現在我的按鈕每次都是第一次點擊。可能值得一試。

1

如果您增加觀看到另一個,嘗試在父視圖設置:

view.setFocusable(false); 

爲我工作。

0

有時我是有問題,當點擊BTNTXTEDT的片段,並且真的可以幫助改用.setOnClickListener()需要.setOnTouchListener這樣的例子:

txtClose.setOnTouchListener((v, event) -> { 
       // do staff... 
       return false; 
      }); 
相關問題