英文:
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("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 inflater.inflate(R.layout.fragment_booksellers, container, false);
}
}
XML (Button part):
<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" />
答案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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论