我正在尝试从片段中的按钮触发操作。该代码并未指出错误,但我的Toast永远不会被触发。
您知道以下方法是否有意义?
这些是我的片段方法:
View.OnClickListener myAction = new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "AccountView", Toast.LENGTH_SHORT).show();
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_account_view, container, false);
accountButton = (Button) view.findViewById(R.id.button);
accountButton.setOnClickListener(myAction);
return inflater.inflate(R.layout.fragment_account_view, container, false);
}
答案 0 :(得分:2)
尝试这个
请用以下代码替换你的onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_account_view, container, false);
accountButton = (Button) view.findViewById(R.id.button);
accountButton.setOnClickListener(myAction);
return view; //change here
}
这是因为您再次创建视图。那就是你要回归inflater.inflate(R.layout.fragment_account_view, container, false);
答案 1 :(得分:1)
您应该返回膨胀的视图:
encodeURIComponent
答案 2 :(得分:1)
返回片段中的视图对象:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_account_view, container, false);
accountButton = (Button) view.findViewById(R.id.button);
accountButton.setOnClickListener(myAction);
return view;
}
用于展示吐司使用:
View.OnClickListener myAction = new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "AccountView", Toast.LENGTH_SHORT).show();
}
};
答案 3 :(得分:0)
gen