Java/Android按钮功能不起任何作用
在Android/Java中的一个按钮上,我在片段中尝试使用setText和onClickListener时遇到了一个问题。setText函数似乎正在工作,但没有更新UI。按下按钮时,onClickListener中的内容根本不会运行。因此,UI似乎由于某种原因与代码断开连接 有关片段和xml的部分代码,请参见下文。非常感谢您的帮助Java/Android按钮功能不起任何作用,java,android,android-fragments,Java,Android,Android Fragments,在Android/Java中的一个按钮上,我在片段中尝试使用setText和onClickListener时遇到了一个问题。setText函数似乎正在工作,但没有更新UI。按下按钮时,onClickListener中的内容根本不会运行。因此,UI似乎由于某种原因与代码断开连接 有关片段和xml的部分代码,请参见下文。非常感谢您的帮助 public class BooksellersFragment extends Fragment{ private Button subscribeButton
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(按钮部分):
您的返回语句是错误的。您必须返回上面创建的视图。因此,请替换:
return inflater.inflate(R.layout.fragment_booksellers, container, false);
与
@user3658609花时间帮助此人毫无意义-他在需要帮助时反应迅速,但大部分问题都没有反馈。有很多未解决的问题。
return inflater.inflate(R.layout.fragment_booksellers, container, false);
return v;