【发布时间】:2017-07-24 12:11:10
【问题描述】:
我正在尝试从片段中的按钮触发操作。代码没有指出错误,但是我的 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);
}
【问题讨论】:
-
在返回过程中,您再次膨胀视图...这会使先前的侦听器无效...
-
因为你返回 inflater.inflate(R.layout.fragment_account_view, container, false);所以进一步初始化所以只返回返回视图。