Android 给Fragment中的控件添加监听及使用intent

 
  
 
  
添加控件 跟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;
        }
        
    }
}
跟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;
        }
        
    }
}

使用intent:
Intent intent = new Intent(getActivity(), 目标.class);

你可能感兴趣的:(android学习,fragment)