0

我的应用程序只有一个活动,我决定将主要内容放在一个片段中,这样我就可以继续将抽屉与其他活动一起使用。但是我不能使按钮工作:

 Button profile= findViewById(R.id.button1);
    profile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplication(),"Sürücüler",Toast.LENGTH_SHORT).show();
        }
    });

    Button education= findViewById(R.id.button2);
    education.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplication(),"Takımlar ve arabalar",Toast.LENGTH_SHORT).show();
        }
    });

    Button health= findViewById(R.id.button3);
    health.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplication(),"Pistler",Toast.LENGTH_SHORT).show();
        }
    });

    Button goals= findViewById(R.id.button4);
    goals.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplication(),"Sıralama",Toast.LENGTH_SHORT).show();
        }
    });

    Button finance= findViewById(R.id.button5);
    finance.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplication(),"Grand Prix Tarihi",Toast.LENGTH_SHORT).show();
        }
    });

    Button comfort= findViewById(R.id.button6);
    comfort.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getApplication(),"GP kart oyunu",Toast.LENGTH_SHORT).show();
        }
    });

如果我将此代码留在 MainActivity 它不起作用(因为尚未创建按钮?)但是当我将它们移动到片段中时,findViewById 和 getApplicationContext 不起作用。如何使 Toast 消息与 Fragment 兼容,如何确保我的 Fragment 可以使用 findviewbyid?

我是初学者,所以如果这很简单,我很抱歉。

任何帮助,将不胜感激。

4

3 回答 3

0

根据您的评论,

return inflater.inflate(R.layout.fragment_home, container, false);

实际上应该是:

View root = inflater.inflate(R.layout.fragment_home, container, false);
Button profile =(Button)root.findViewById(R.id.button1);
profile.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(getApplication(),"Sürücüler",Toast.LENGTH_SHORT).show();
    }
});

//similarly for other buttons

return root;
于 2018-08-20T17:49:38.903 回答
0

如果您可以使用片段 XML 中的按钮

 Button profile= v.findViewById(R.id.button1);

所以 v 是您在“onCreateView”方法中的片段视图

View v=inflater.inflate(R.layout.fragment_home, container, false);

在方法的最后

return v;
于 2018-08-20T17:50:25.137 回答
0

片段通常用作活动用户界面的一部分,并为活动贡献自己的布局。因此,首先您需要为包含所有按钮的片段创建一个布局。

要为片段提供我们创建的布局,您必须在片段类中实现 onCreateView() 回调方法,Android 系统会在片段绘制其布局时调用该方法。此方法的实现必须返回一个 View,它是片段布局的根。

public  class ExampleFragment extends Fragment {

 Button health;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View root = inflater.inflate(R.layout.example_fragment, container, false);
    // bind views like this:-
    health= root.findViewById(R.id.button3);

    return  root;  
  }
}

要管理 Activity 中的 Fragment,您需要使用 FragmentManager。要获取它,请从您的活动中调用 getSupportFragmentManager()。现在活动类添加以下代码以将片段添加到活动,:-

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

 // Commit the transaction
 transaction.commit();

R.id.fragment_container在活动布局中要添加片段的容器布局的id在哪里,通常是框架布局。

如何在android中使用片段

于 2018-08-20T17:58:02.150 回答