我用3个标签创建了这个应用程序,标签工作正常但是应用于tab1的按钮没有响应点击。无论如何都试过,找不到任何错误。我的代码有什么问题吗?
Tab1.java
public class Tab1 extends Fragment {
int click = 0;
View view;
Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view = inflater.inflate(R.layout.tab1, container, false);
button = (Button)view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Button is clicked","Tab1");
if(click == 0){
MainActivity.swipe = 1;
click = 1;
}
else if(click == 1){
MainActivity.swipe = 0;
click = 0;
}
}
});
return inflater.inflate(R.layout.tab1,container,false);
}
}
这是XML文件
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Tab1"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_gravity="center_horizontal" />
答案 0 :(得分:1)
尝试return view;
代替return inflater.inflate(R.layout.tab1,container,false);