有人能告诉我如何在选项卡式片段布局中设置一个按钮来打开一个新活动吗?如果我在选项卡中设置一个按钮,那么它会显示错误。
有人能告诉我如何在选项卡式片段布局中设置一个按钮来打开一个新活动吗?如果我在选项卡中设置一个按钮,那么它会显示错误。
您必须将膨胀视图保存在变量中,然后在该视图上使用 findViewById 来查找 textview 和按钮,最后返回膨胀视图。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_g, container, false);
TextView tv = (TextView) root.findViewById(R.id.textView);
Button btn = (Button) root.findViewById(R.id.button);
//Other code here
return root;
}
您误解了一些非常基本的事情,例如在 return 语句之后编写代码。您可能想在线阅读一些入门指南。
您可以使用以下代码
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_complete_registration, container, false);
final TextView tv=(TextView)view.findViewById(R.id.textView);
final Button button=(Button)view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Your required code here
}
});
return view;
}