android中片段中按钮的按钮侦听器 [英] Button Listener for button in fragment in android

查看:27
本文介绍了android中片段中按钮的按钮侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 新手,正在尝试自学.但是我在使用 Fragments 时遇到了困难.我正在创建一个简单的应用程序来学习片段.我认为这可能看起来很愚蠢,但我真的无法让它发挥作用.

I am new to Android and trying to learn on my own. But I am having a tough time with Fragments. I am creating a simple application to learn fragments. I think it may seem silly but I really can't get this to work.

我想要做的就是单击 Fragment_One 中的按钮 (buttonSayHi),Fragment_One 应替换为 Fragment_Two.

All I want to do is on the click of a button (buttonSayHi) in Fragment_One, Fragment_One should be replaced by Fragment_Two.

我不确定何时调用 Fragment 代码以及我应该在哪里编写代码来调用第二个片段.我收到错误消息:无法启动活动组件.

I am not sure when the Fragment code is called and where am I supposed to write my code to call the second fragment. I get the error: Unable to start Activity component.

但是,如果我放弃按钮的侦听器并且片段显示在活动中,则代码可以正常工作.

However, the code works fine if I discard the listener for button and the fragment is displayed within the activity.

我做了大量研究,阅读了 developer.android.com 上的片段教程以及 Lars Vogella 的教程.我觉得我的概念不清楚.

I have done considerable research, read the fragments tutorial on developer.android.com and also the tutorial by Lars Vogella. I think my concepts are not clear.

任何帮助将不胜感激.

代码如下:

activity_main.xml

activity_main.xml

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

</FrameLayout>

fragment_one.xml

fragment_one.xml

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

    <EditText
        android:id="@+id/editTextPersonName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/buttonSayHi"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Say Hi" 
        android:onClick="onButtonClicked"/>

</LinearLayout>

fragment_two.xml

fragment_two.xml

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

    <TextView
        android:id="@+id/textViewResult"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="I will say Hi!" />

</LinearLayout>

MainActivity.java

MainActivity.java

package com.example.fragmenttutorial;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

public class MainActivity extends Activity{

    View view;
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);     

        Fragment fragmentOne = new FragmentOne();

        fragmentTransaction.add(R.id.frameLayoutFragmentContainer, fragmentOne);
        fragmentTransaction.addToBackStack(null);

        fragmentTransaction.commit();
    }

    protected void onButtonClicked()
    {
        if(view.getId() == R.id.buttonSayHi){
            Fragment fragmentTwo = new FragmentTwo();

            fragmentTransaction.replace(R.id.frameLayoutFragmentContainer, fragmentTwo);
            fragmentTransaction.addToBackStack(null);

            fragmentTransaction.commit();   

        }

    }
}

FragmentOne.java

FragmentOne.java

package com.example.fragmenttutorial;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentOne extends Fragment{

    View view;
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

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

        View view = inflater.inflate(R.layout.fragment_one, container, false);
        return view;
    }

//  protected void onButtonClicked()
//  {
//      if(view.getId() == R.id.buttonSayHi){
//          Fragment fragmentTwo = new FragmentTwo();
//
//          fragmentTransaction.replace(R.id.frameLayoutFragmentContainer, fragmentTwo);
//          fragmentTransaction.addToBackStack(null);
//
//          fragmentTransaction.commit();   
//
//      }
//
//  }
}

我已经注释掉了片段中的点击代码.我也尝试在片段中实现 onClickListener.

I have commented out the on click code in the fragment. I have also tried implementing the onClickListener in the fragment.

FragmentTwo.java

FragmentTwo.java

package com.example.fragmenttutorial;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentTwo extends Fragment{

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

        View view = inflater.inflate(R.layout.fragment_two, container, false);
        return view;    
    }
}

我已经从我的 XML 代码中删除了 android:onClick="onButtonClicked" 行.并编辑了以下文件,但仍然不起作用.你们能给我提供一个没有 android:onClick="onButtonClicked" 行的工作示例吗.

I have removed the line android:onClick="onButtonClicked" from my code in XML. And edited the following files but it still does not work. Can you guys provide me a working example without the android:onClick="onButtonClicked" line.

MainActivity.java

MainActivity.java

package com.example.fragmenttutorial;

import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

public class MainActivity extends Activity{

    View view;
    Fragment fragmentOne;
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);     

        fragmentOne = new FragmentOne();

        fragmentTransaction.add(R.id.frameLayoutFragmentContainer, fragmentOne);
        fragmentTransaction.addToBackStack(null);

        fragmentTransaction.commit();       
    }
}

FragmentOne.java

FragmentOne.java

package com.example.fragmenttutorial;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class FragmentOne extends Fragment implements OnClickListener{

    View view;
    Fragment fragmentTwo;
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

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

        View view = inflater.inflate(R.layout.fragment_one, container, false);
        Button buttonSayHi = (Button) view.findViewById(R.id.buttonSayHi);
        buttonSayHi.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {

        fragmentTwo = new FragmentTwo();

        fragmentTransaction.replace(R.id.frameLayoutFragmentContainer, fragmentTwo);
        fragmentTransaction.addToBackStack(null);

        fragmentTransaction.commit();   

    }
}

感谢您提出宝贵的建议.

Thank you for your valuable suggestions.

推荐答案

你的片段类应该实现 OnClickListener

Your fragment class should implement OnClickListener

public class SmartTvControllerFragment extends Fragment implements View.OnClickListener

然后像下面的例子一样获取视图、链接按钮并设置 onClickListener

Then get view, link button and set onClickListener like in example below

 View view;

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

    view = inflater.inflate(R.layout.smart_tv_controller_fragment, container, false);
    upButton = (Button) view.findViewById(R.id.smart_tv_controller_framgment_up_button);
    upButton.setOnClickListener(this);
    return view;
 }

然后添加 onClickListener 方法并执行您想要的操作.

And then add onClickListener method and do what you want.

@Override
public void onClick(View v) {
 //do what you want to do when button is clicked
    switch (v.getId()) {
        case R.id.textView_help:
            switchFragment(HelpFragment.TAG);
            break;
        case R.id.textView_settings:
            switchFragment(SettingsFragment.TAG);
            break;
    }
}

这是我的代码示例,但希望你能理解

This is my example of code, but I hope you understood

这篇关于android中片段中按钮的按钮侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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