Android 怎么给Fragment中的控件添加监听?
展开全部
跟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;
}
}
}
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;
}
}
}
微测检测5.10
2023-05-10 广告
2023-05-10 广告
您好!建议咨 深圳市微测检测有限公司,已建立起十余个专业实验室,企业通过微测检测就可以获得一站式的测试与认 证解决方案;(EMC、RF、MFi、BQB、QI、USB、安全、锂电池、快充、汽车电子EMC、汽车手机互 联、语音通话质量),认证遇...
点击进入详情页
本回答由微测检测5.10提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询