Android中Fragment中启动一个Activity,实例化控件。

相信大家对于在一个Activity中启动另一个Activity非常熟悉,具体代码如下。

searchView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mintent=new Intent(MainActivity.this,SearchActivity.class);
                startActivity(mintent);
                overridePendingTransition(R.animator.animator_in,R.animator.animator_out);
            }
        });
但是在一个Fragment中实现开启新的Activity中就不是这样了,
package com.example.app_weixin;

import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by 尽途 on 2017/3/25.
 */

public class Fragment_profile extends Fragment {
    private RelativeLayout userinfo_re;
    private TextView userName_tv,account_tv;
    private ImageView userImage_iv;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_profile,container,false);
        initView(view);
        initData();
        return view;
    }
    void initView(View view){
        userinfo_re=(RelativeLayout)view.findViewById(R.id.userinfo);//在Fragment中实例化控件
        userImage_iv=(ImageView)view.findViewById(R.id.touxiangiv);
        userName_tv=(TextView)view.findViewById(R.id.username);
        account_tv=(TextView)view.findViewById(R.id.account_tv);
    }
    void initData(){
        userImage_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mintent=new Intent(getActivity(),Activity_profile_account.class);//在Fragment中实现开启新的Activity
                startActivity(mintent);
            }
        });
    }
}

具体的代码如下


你可能感兴趣的:(Android)