[英]onClickListener is not called from the Fragment
In the following piece of code, even after registering an onClickLister, callback function is not getting called. 在下面的代码中,即使在注册onClickLister之后,也不会调用回调函数。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view1 = inflater.inflate(R.layout.fragment_register,
container, false);
final Button button =
(Button) view1.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
updateUser(v);
}
});
return view1;
}
public void updateUser(View v) {
Toast.makeText(getActivity(), "name", Toast.LENGTH_LONG).show();
}
and the fragment.xml file will be, Registering an onClick Listener in the xml also doesnt help, because it calls the function in the Activity. 并且fragment.xml文件将是,在xml中注册onClick侦听器也无济于事,因为它在Activity中调用了该函数。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.picturejumble.Register$PlaceholderFragment" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="34dp"
android:layout_marginTop="37dp"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="54dp"
android:ems="10"
android:inputType="textEmailAddress" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginLeft="50dp"
android:layout_marginTop="60dp"
android:text="Button" />
</RelativeLayout>
Logcat doesn't report any errors, and the code doesn't crashes as well. Logcat不会报告任何错误,并且代码也不会崩溃。
The problem is that the callback is not getting called. 问题是没有调用回调。
Just call your updateUser(View v)
method in your xml file for button onClick
method instead of in programming. 只需在xml文件中为按钮
onClick
方法调用updateUser(View v)
方法即可,而不是在编程中。
Use this 用这个
android:onClick = "updateUser"
for your Button
in xml file. 用于xml文件中的
Button
。
no need to pass the view parameter 无需传递view参数
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
updateUser();
}
});
public void updateUser() {
Toast.makeText(getActivity(), "name", Toast.LENGTH_LONG).show();
}
use this line of code in onCreateView before inflating view super.onCreateView(inflater, container, savedInstanceState);
在扩大视图
super.onCreateView(inflater, container, savedInstanceState);
之前,请在onCreateView中使用以下代码行super.onCreateView(inflater, container, savedInstanceState);
if it still dosn't work please paste full code for fragment class 如果仍然无法正常工作,请为片段类粘贴完整的代码
Please try below code and let me know: 请尝试以下代码,并让我知道:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
updateUser(v);
}
});
}
Define this Button
outside the onCreateView
method 在
onCreateView
方法之外定义此Button
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.