button的onclick方法应该写在哪个位置?

suhaihui 2014-06-29 10:18:51
button的设置如下:
<LinearLayout

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/play"
android:onClick="play_move"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="play"/>
<Button
android:id="@+id/stop"
android:onClick="stop_move"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="stop"/>
</LinearLayout>

MainActivity里面的程序如下:

public static class PlaceholderFragment extends Fragment {


public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.farme_layout, container,
false);
m1=(ImageView) rootView.findViewById(R.id.stop_icon);
return rootView;
}
public void stop_move(View view){
m1.setVisibility(View.INVISIBLE);
}
public void play_move(View view){
m1.setVisibility(View.VISIBLE);
}
}

可是我点击按键就报没有 stop_move, play_move 的函数。是不是我的位置写错了?
...全文
454 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
网络咖啡 2014-07-05
  • 打赏
  • 举报
回复
可能Fragment 不支持这种写法吧,只有Activity才支持
鱼公 2014-07-05
  • 打赏
  • 举报
回复
我只知道activity可以这样,fragment还是老老实实用setonclicklistener吧
bloodGolem 2014-07-05
  • 打赏
  • 举报
回复
不懂。。。。。。。。。。。。。。。。。。。。。
  • 打赏
  • 举报
回复
还是用setOnClickListener吧
jemhoo 2014-07-05
  • 打赏
  • 举报
回复
应该是 写错地方了 可以试试 在 onActivityCreated 里看行不行 看看 fragment 的生命周期
jeky_zhang2013 2014-07-04
  • 打赏
  • 举报
回复
还没这样写过,看下logcat是什么异常
manxiSafe 2014-07-04
  • 打赏
  • 举报
回复
今天看了你的帖子我才知道有这种方法,先去充实下再说 。。。
奋斗中的显摆 2014-06-29
  • 打赏
  • 举报
回复
.....我从没这么写过....! 如果你想简单 我介绍你个xUtils框架

80,348

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧