I have to click the button twice for it to work

前端 未结 8 1816
温柔的废话
温柔的废话 2020-12-05 04:33

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

相关标签:
8条回答
  • 2020-12-05 04:53

    Sometime I had it problem when click on btn or txt or edt on the fragment, and realy helps use instead .setOnClickListener() need .setOnTouchListener like this example:

    txtClose.setOnTouchListener((v, event) -> {
                    // do staff...
                    return false;
                });
    
    0 讨论(0)
  • 2020-12-05 04:53

    try this once
    UPDATED

    override onResume method in your activity and then clear focus from your edittext and set focus to your main layout

    edittext.clearFocus();
    layout.requestFocus();
    

    or

    button.requestFocus();
    

    if the problem is with virtual keyboard it might help you

    in your AndroidManifest.xml file inside your Activity tag put this line

    android:windowSoftInputMode="stateHidden"
    
    0 讨论(0)
  • 2020-12-05 04:55

    Simply use getText() to get text from EditText And then set text of TextView using setText().

    0 讨论(0)
  • 2020-12-05 05:03

    My problem was the Button XML defining:

    android:focusableInTouchMode="true"
    

    Remove this attribute and the button doesn't require being touched twice. It appears as though the first touch is consumed to assign focus on the button and the second then triggers the OnClickListener.

    Note that the Button works without issue with the android:focusable="true" attribute.

    0 讨论(0)
  • 2020-12-05 05:03

    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
    
    0 讨论(0)
  • 2020-12-05 05:11

    By adding the following lines :

     <Button
        android:id="@+id/sauvegarder"
        android:text="Sauvegarder"
        android:layout_gravity="center"
        android:background="@color/colorAccent"
        android:layout_margin="@dimen/fontmargin"
        style="@style/edit"
        android:gravity="center"
        android:textColor="@color/colorPrimary"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="false"
        />
    

    in my xml file it works perfectly !

    0 讨论(0)
提交回复
热议问题