按钮监听器在android中的片段中的按钮

Bot*_*Bot 32 android android-fragments

我是Android新手,并试图自己学习.但我正在与Fragments度过艰难时期.我正在创建一个简单的应用程序来学习片段.我觉得这看起来很傻但我真的无法让它发挥作用.

我想要做的只是点击Fragment_One中的按钮(buttonSayHi),Fragment_One应该被Fragment_Two取代.

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

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

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

任何帮助将不胜感激.

以下是代码:

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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();   

        }

    }
}
Run Code Online (Sandbox Code Playgroud)

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();   
//
//      }
//
//  }
}
Run Code Online (Sandbox Code Playgroud)

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

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;    
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:我从我的XML代码中删除了android:onClick ="onButtonClicked"行.并编辑了以下文件,但它仍然无法正常工作.你能为我提供一个没有android:onClick ="onButtonClicked"行的工作示例.

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();       
    }
}
Run Code Online (Sandbox Code Playgroud)

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();   

    }
}
Run Code Online (Sandbox Code Playgroud)

感谢您提出宝贵意见.

Rag*_*sis 64

您的片段类应该实现OnClickListener

public class SmartTvControllerFragment extends Fragment implements View.OnClickListener
Run Code Online (Sandbox Code Playgroud)

然后获取视图,链接按钮并设置onClickListener,如下例所示

 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;
 }
Run Code Online (Sandbox Code Playgroud)

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

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 但这不是op想要的 (5认同)
  • 我更喜欢在片段内部编写onClick Listeners,而不是将其放在XML中.更容易找到onClick方法的位置以及它正在做什么.所以我建议在xml中删除该行:) (3认同)
  • 但是op在xml中有`android:onClick ="onButtonClicked" (2认同)

Tom*_*ras 8

使用您的代码

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;
    }
Run Code Online (Sandbox Code Playgroud)

但我认为以这种方式更好地处理按钮:

@Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.buttonSayHi:
            /** Do things you need to..
               fragmentTwo = new FragmentTwo();

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

               fragmentTransaction.commit();  
            */
            break;
        }   
    }
Run Code Online (Sandbox Code Playgroud)