gpt4 book ai didi

java - 安卓代码: clarify button clicking facebook url must display

转载 作者:太空宇宙 更新时间:2023-11-04 14:59:31 25 4
gpt4 key购买 nike

friend 们,我已经编写了显示苹果的 facebook url 的代码。我在代码中使用了 fragment

package com.coded.sandeep;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BioFragment extends Fragment {



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.bio, container, false);

return rootView;
}}

对于上面的代码,我必须添加按钮单击事件,因此我添加了如下所示的事件,但它:

不工作,我无法正确使用 onclick 事件,任何人都可以帮我修改代码

package com.coded.sandeep;
import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class MainFragment extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.activity_am, container, false);

return rootView;
}}
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);

ImageButton amelia_facebook = (ImageButton)rootView.findViewById(R.id.facebook_am);
amelia_facebook.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(getActivity(),Facebook.class));
}

});

}}

当我刚接触 Android 时,请帮助我完成上面的代码

最佳答案

onCreateview中初始化

ImageButton btnEnter = (ImageButton)rootView.findViewById(R.id.facebook1);

并移动与onCreateView相关的代码。该按钮可能属于 fragment 布局

其次,您会膨胀 activity_main.xml。我不确定您是否膨胀了正确的布局,因为它看起来布局属于 MainActivity

编辑:

public class MainFragment extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.activity_am, container, false);
ImageButton amelia_facebook = (ImageButton)rootView.findViewById(R.id.facebook_am);
amelia_facebook.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(getActivity(),Facebook.class));
}

});
return rootView;
}

}

关于java - 安卓代码: clarify button clicking facebook url must display,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22779139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com