android onClick按钮里面的片段不起作用,没有错误

时间:2015-10-19 03:33:31

标签: java android xml android-layout android-fragments

我用一些按钮创建了一个片段,我试图通过点击按钮调用其他片段,但没有任何反应,没有错误,没有操作。

这是我的实施:

main_activit.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/grey"
    tools:context=".MainActivity"
    android:weightSum="1"
    android:orientation="vertical"
    >

    <android.support.v7.widget.Toolbar
        android:id="@+id/mainToobar"
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:elevation="4dp"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:title="@string/mainToobarTitle"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        />

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/homebuttons_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="200dp" />

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

HomeButtonsFragment.java

public class HomeButtonsFragment extends Fragment implements View.OnClickListener {

    @Override
    public View onCreateView(
            LayoutInflater inflater,
            ViewGroup container,
            Bundle savedInstanceState
    ) {
        View view = inflater.inflate(R.layout.fragment_home_buttons, container, false);

        Button button = (Button) view.findViewById(R.id.schedule_home_button);
        button.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        ScheduleFragment scheduleFragment = new ScheduleFragment();
        fragmentTransaction.add(R.id.main_fragment_container, scheduleFragment);
    }
}

fragment_home_button.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

    <Button
        android:id="@+id/schedule_home_button"
        android:layout_width="100dp"
        android:layout_height="fill_parent"
        android:layout_alignParentEnd="true"
        android:text="@string/label.home.button.schedule"
        android:tag="home_button"
        android:stateListAnimator="@null"
        style="@style/ScheduleButton"
        />

</RelativeLayout>

4 个答案:

答案 0 :(得分:0)

您在.commit()声明中遗漏了fragmentTransaction

 fragmentTransaction.add(R.id.main_fragment_container, scheduleFragment).commit();

答案 1 :(得分:0)

你错过了 commit(); ,我确信有时会遗漏一些小事,只需添加 commit(); ,它就能正常工作 用这个  FragmentManager fragmentManager = getFragmentManager();             FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class TestShop {

    public static final String MALBORO_LIGHTS = "Malboro Lights";
    public static final String MALBORO_LIGHTS_SOFT = "Marlboro Lights, Soft Pack";
    public static final String MALBORO_LIGHTS_FLIP = "Marlboro Lights, Flip Pack";

    public static final String MALBORO_RED = "Malboro Reds";
    public static final String MALBORO_RED_SOFT = "Marlboro Reds, Soft Pack";
    public static final String MALBORO_RED_FLIP = "Marlboro Reds, Flip Pack";

    public static final String DUNHILL = "Dunhill";
    public static final String DUNHILL_SOFT = "Dunhill, Soft Pack";
    public static final String DUNHILL_FLIP = "Dunhill, Flip Pack";

    public static final String PHILLIP = "Phillip";
    public static final String PHILLIP_SOFT = "Phillip, Soft Pack";
    public static final String PHILLIP_FLIP = "Phillip, Flip Pack";

    public static void main(String[] args) {
        new TestShop();
    }

    public TestShop() {
        Scanner keyboard = new Scanner(System.in);

        /**************************************************************************/
        /* This part is dynamic, change this when you want new items, prices or   */
        /* groups                                                                 */
        /**************************************************************************/

        // A list of items that have been ordered
        Map<String, Integer> orders = new HashMap<>(25);

        // A list of items keyed to prices
        Map<String, Double> prices = new HashMap<>(25);
        prices.put(MALBORO_LIGHTS_SOFT, 70d);
        prices.put(MALBORO_LIGHTS_FLIP, 80d);
        prices.put(MALBORO_RED_SOFT, 70d);
        prices.put(MALBORO_RED_FLIP, 80d);
        prices.put(DUNHILL_SOFT, 60d);
        prices.put(DUNHILL_FLIP, 70d);
        prices.put(PHILLIP_SOFT, 70d);
        prices.put(PHILLIP_FLIP, 80d);

        // Mapping items to groups...
        Map<String, List<String>> groups = new HashMap<>(25);
        groups.put(MALBORO_LIGHTS, new ArrayList<>(Arrays.asList(new String[]{MALBORO_LIGHTS_SOFT, MALBORO_LIGHTS_FLIP})));
        groups.put(MALBORO_RED, new ArrayList<>(Arrays.asList(new String[]{MALBORO_RED_SOFT, MALBORO_RED_FLIP})));
        groups.put(DUNHILL, new ArrayList<>(Arrays.asList(new String[]{DUNHILL_SOFT, DUNHILL_FLIP})));
        groups.put(PHILLIP, new ArrayList<>(Arrays.asList(new String[]{PHILLIP_SOFT, PHILLIP_FLIP})));

        /**************************************************************************/

        /**************************************************************************/
        /* The rest of this is pretty static and is driven by the data from above */
        /* This means, you don't need change anything below here, when the stuff  */
        /* changes                                                                */
        /**************************************************************************/

        boolean done = false;
        do {
            System.out.println("Welcome to my shop");
            List<String> keys = new ArrayList<>(groups.keySet());
            for (int index = 0; index < keys.size(); index++) {
                System.out.println("[" + (index + 1) + "] " + keys.get(index));
            }
            System.out.println("[0] Exit");
            String input = keyboard.nextLine();
            try {
                int selectedIndex = Integer.parseInt(input);
                if (selectedIndex == 0) {
                    done = true;
                } else if (selectedIndex > 0 && selectedIndex <= keys.size()) {
                    String key = keys.get(selectedIndex - 1);

                    List<String> items = groups.get(key);
                    boolean subDone = false;
                    do {
                        System.out.println("Items for " + key + "....");
                        for (int index = 0; index < items.size(); index++) {
                            System.out.println("  [" + (index + 1) + "] " + items.get(index));
                        }
                        System.out.println("  [0] Return");
                        input = keyboard.nextLine();
                        try {
                            int index = Integer.parseInt(input);
                            if (index > 0 && index <= items.size()) {
                                index--; // The items in the list are 0 indexed
                                String item = items.get(index);
                                Integer quanity = orders.get(item);
                                if (quanity == null) {
                                    quanity = 1;
                                } else {
                                    quanity++;
                                }
                                orders.put(item, quanity);
                            } else if (index == 0) {
                                subDone = true;
                            } else {
                                System.out.println("Invalid selection, please try again");
                            }
                        } catch (NumberFormatException exp) {
                            System.out.println("!! " + input + " is not a valid selection");
                        }
                    } while (!subDone);
                } else {
                    System.out.println("Invalid selection, please try again");
                }
            } catch (NumberFormatException exp) {
                System.out.println("!! " + input + " is not a valid selection");
            }

        } while (!done);

        double total = 0;
        for (Map.Entry<String, Integer> entry : orders.entrySet()) {

            int quanity = entry.getValue();
            double price = prices.get(entry.getKey());

            total += quanity * price;

            System.out.println(String.format(
                    "%-20s x %4d @ %7s = %10s",
                    entry.getKey(),
                    quanity,
                    NumberFormat.getCurrencyInstance().format(price),
                    NumberFormat.getCurrencyInstance().format(quanity * price)));

        }

        System.out.printf("%-20s   %4s   %7s   ==========%n", "", "", "");
        System.out.printf("%-20s   %4s   %7s   %10s%n", "", "", "", NumberFormat.getCurrencyInstance().format(total));
    }

}

答案 2 :(得分:0)

@覆盖     public void onClick(查看v){         FragmentManager fragmentManager = getFragmentManager();         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    ScheduleFragment scheduleFragment = new ScheduleFragment();
    fragmentTransaction.add(R.id.main_fragment_container, scheduleFragment).commit();

fragmentTransaction.commit();     }

答案 3 :(得分:-1)

你的onClick方法应该是,

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.schedule_home_button){
           FragmentManager fragmentManager = getFragmentManager();
           FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

           ScheduleFragment scheduleFragment = new ScheduleFragment();
           fragmentTransaction.add(R.id.main_fragment_container, scheduleFragment).commit();
        }
    }