如何从Android中的片段上单击按钮打开片段 [英] How to open a Fragment on button click from a fragment in Android

查看:74
本文介绍了如何从Android中的片段上单击按钮打开片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的HomeFragment上有一些按钮,我想在单击片段时打开一个Fragment.导航之类的东西.我正在尝试使用下面的代码,但是没有用.请帮忙 !我是android的新手,正在尝试学习新事物.

I have some button on my HomeFragment i want to to open a Fragment on button click from a fragment . Something like Navigation. I am trying with bellow code but did not worked. Please help ! I am very new to android and trying to learn new things.

这是我的代码

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class HomeFragment extends Fragment {

    public HomeFragment(){}



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

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

        Button aboutBtn = (Button) rootView.findViewById(R.id.aboutusButton);
        Button phonebookBtn = (Button) rootView.findViewById(R.id.phbookButton);
        Button schemeBtn = (Button) rootView.findViewById(R.id.schemeButton);
        Button loanBtn = (Button) rootView.findViewById(R.id.loanButton);
        Button serviceBtn = (Button) rootView.findViewById(R.id.serviceButton);
        Button devBtn = (Button) rootView.findViewById(R.id.devButton);

        aboutBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                // Launching new Activity on selecting single List Item
                Intent i = new Intent(getActivity(), AboutFragment.class);
                startActivity(i);
            }
        });

        phonebookBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                // Launching new Activity on selecting single List Item
                Intent j = new Intent(getActivity(), PhoneBookFragment.class);
                startActivity(j);
            }
        });

        schemeBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                // Launching new Activity on selecting single List Item
                Intent k = new Intent(getActivity(), HomeFragment.class);
                startActivity(k);
            }
        });

        loanBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                // Launching new Activity on selecting single List Item
                Intent l = new Intent(getActivity(), RemittanceFragment.class);
                startActivity(l);
            }
        });

        serviceBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                // Launching new Activity on selecting single List Item
                Intent m = new Intent(getActivity(), ServiceFragment.class);
                startActivity(m);
            }
        });

        devBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                // Launching new Activity on selecting single List Item
                Intent n = new Intent(getActivity(), AboutDeveloper.class);
                startActivity(n);
            }
        });


        return rootView;
    }
}

我正在寻找波纹管代码之类的东西,但不知道如何使代码与我的代码一起使用.

I am looking for something like bellow code but don't know how to make the code works with my code.

Button aboutBtn = (Button) v.findViewById(R.id.aboutButton);
aboutBtn.setOnClickListener(this);

Button homeBtn = (Button) v.findViewById(R.id.homeButton);
homeButton.setOnClickListener(this);

Button serviceBtn = (Button) v.findViewById(R.id.serviceButton);
serviceBtn.setOnClickListener(this);

@Override
public void onClick(View view) {
    Fragment fragment = null;
    switch (view.getId()) {
        case aboutButton:
            fragment = new AboutFragment();
            break;

        case homeBtn:
            fragment = new PhonebookFragment();
            break;

        case serviceBtn:
            fragment = new ServiceFragment();
            break;

        default:
            fragment = new HomeFragment();
            break;

    }
}

推荐答案

您可以在单击按钮时使用FragmentTransaction替换片段.像这样:

You can replace the fragment using FragmentTransaction on button click. Something like this:

  Fragment someFragment = new SomeFragment(); 
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container, someFragment ); // give your fragment container id in first parameter
    transaction.addToBackStack(null);  // if written, this transaction will be added to backstack
    transaction.commit(); 

在此处了解有关执行片段交易的信息.

代码:

  package com.rupomkhondaker.sonalibank;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class HomeFragment extends Fragment implements View.OnClickListener {

    public HomeFragment() {
    }

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

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

        Button aboutBtn = (Button) rootView.findViewById(R.id.aboutusButton);
        Button phonebookBtn = (Button) rootView.findViewById(R.id.phbookButton);

        aboutBtn.setOnClickListener(this);
        phonebookBtn.setOnClickListener(this);


        return rootView;
    }

    @Override
    public void onClick(View view) {
        Fragment fragment = null;
        switch (view.getId()) {
            case R.id.aboutusButton:
                fragment = new AboutFragment();
                replaceFragment(fragment);
                break;

            case R.id.phbookButton:
                fragment = new PhoneBookFragment();
                replaceFragment(fragment);
                break;
        }
    }

    public void replaceFragment(Fragment someFragment) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_container, someFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }


}

这篇关于如何从Android中的片段上单击按钮打开片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆