Java/Android按钮功能无响应

huangapple go评论72阅读模式
英文:

Java/Android Button functions do nothing

问题

以下是翻译好的内容:

public class BooksellersFragment extends Fragment {

    private Button subscribeButton;

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_booksellers, container, false);

        subscribeButton = (Button) v.findViewById(R.id.subscribeButton);

        System.out.println("This runs");

        System.out.println(subscribeButton.getText()); // This shows the default text
        subscribeButton.setText("testing");
        System.out.println(subscribeButton.getText()); // This outputs the new text but the button text doesnt change in the UI.

        subscribeButton.setOnClickListener(v1 -> {
            System.out.println("nothing here runs when clicking the button");
            subscribeButton.setText("test");
        });
        return v; // Fix: Return the inflated view 'v' instead of reinflating the layout
    }
}

XML (Button 部分):

<Button
    android:id="@+id/subscribeButton"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="20dp"
    android:layout_marginRight="20dp"
    android:text="Prenumerera"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/bookImage" />

请注意,我已经移除了您提供的代码中的HTML实体编码,并修复了在onCreateView方法中返回视图的问题。

英文:

I have a problem trying to use setText and onClickListener on a button in Android/Java inside a fragment. The setText function seems to be working but doesnt update the UI. Whats inside the onClickListener doesnt run at all when pressing the button. So seems that the UI is disconnected from the code for some reason.

See below for part of the code for the fragment and the xml. Any help is highly appreciated!

public class BooksellersFragment extends Fragment{

private Button subscribeButton;

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_booksellers, container, false);

    subscribeButton = (Button) v.findViewById(R.id.subscribeButton);

    System.out.println(&quot;This runs&quot;);

    System.out.println(subscribeButton.getText()); //This shows the default text
    subscribeButton.setText(&quot;testing&quot;);
    System.out.println(subscribeButton.getText()); //This outputs the new text but the button text doesnt change in the UI.

    subscribeButton.setOnClickListener(v1 -&gt; {
        System.out.println(&quot;nothing here runs when clicking the button&quot;);
        subscribeButton.setText(&quot;test&quot;);
    });
    return inflater.inflate(R.layout.fragment_booksellers, container, false);
   }
}

XML (Button part):

&lt;Button
    android:id=&quot;@+id/subscribeButton&quot;
    android:layout_width=&quot;0dp&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_marginStart=&quot;20dp&quot;
    android:layout_marginLeft=&quot;20dp&quot;
    android:layout_marginTop=&quot;10dp&quot;
    android:layout_marginEnd=&quot;20dp&quot;
    android:layout_marginRight=&quot;20dp&quot;
    android:text=&quot;Prenumerera&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toBottomOf=&quot;@+id/bookImage&quot; /&gt;

答案1

得分: 1

你的返回语句是错误的。你必须返回你上面创建的视图。所以用以下代码替换:

return inflater.inflate(R.layout.fragment_booksellers, container, false);

用以下代码:

return v;
英文:

Your return statement is wrong. You have to return the view you created above. So replace:

return inflater.inflate(R.layout.fragment_booksellers, container, false);

with

return v;

huangapple
  • 本文由 发表于 2020年9月17日 22:27:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63940238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定