gpt4 book ai didi

android - android中 fragment 中按钮的按钮监听器

转载 作者:IT老高 更新时间:2023-10-28 21:45:51 27 4
gpt4 key购买 nike

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

我只想在 Fragment_One 中单击一个按钮 (buttonSayHi),Fragment_One 应该被 Fragment_Two 替换。

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

但是,如果我放弃按钮的监听器并且 fragment 显示在 Activity 中,则代码可以正常工作。

我进行了大量研究,阅读了 developer.android.com 上的 fragment 教程以及 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>

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

<?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

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

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

我已经注释掉了 fragment 中的点击代码。我也尝试在 fragment 中实现 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;
}
}

编辑:我已经从我的 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();
}
}

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

}
}

感谢您的宝贵建议。

最佳答案

你的 fragment 类应该实现 OnClickListener

public class SmartTvControllerFragment extends Fragment implements View.OnClickListener

然后获取 View 、链接按钮并设置 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;
}

然后添加 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;
}
}

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

关于android - android中 fragment 中按钮的按钮监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18711433/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com