Android 怎么给Fragment中的控件添加监听?

 我来答
活宝顺风
2013-03-30
知道答主
回答量:5
采纳率:0%
帮助的人:5.6万
展开全部
跟activity一样的设置
package com.huaao.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class BtFragment extends Fragment implements OnClickListener {
private Button login;
private Button reg;

@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
Log.i("i"," Fragment 执行onAttach");

}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.i("i"," Fragment 执行onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.i("i"," Fragment 执行onCreateView");
View view = inflater.inflate(R.layout.main1, null, false);
login = (Button) view.findViewById(R.id.login);
reg = (Button) view.findViewById(R.id.reg);
login.setOnClickListener(this);
reg.setOnClickListener(this);
return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Log.i("i"," Fragment 执行onActivityCreated");

}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i("i"," Fragment 执行onStart");
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.i("i"," Fragment 执行onPause");
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i("i"," Fragment 执行onResume");
}

@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.i("i"," Fragment 执行onStop");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("i", " Fragment 执行onDestroy");
}
@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
Log.i("i"," Fragment 执行onDetach");
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
switch (id) {
case R.id.login:
((MainActivity) getActivity()).showLogin();
break;
case R.id.reg:
//点击时要执行的操作 放在MainActivity 的showReg()方法中

((MainActivity)getActivity()).showReg();
break;
default:
break;
}

}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式