Android 使用片段内的按钮更改活动

Android 使用片段内的按钮更改活动,android,android-fragments,Android,Android Fragments,我看过一些关于片段的教程,其中创建了一个主活动,并使用按钮逐个更改片段。我的问题是,我可以使用被调用布局的按钮而不是主活动的按钮来更改片段吗。这就是我目前的困境。以前,我的应用程序运行时使用意图更改活动。但我遇到了一个问题,我需要使用片段来解决它 这是我的“主要”片段代码: public class FragmentMain extends Activity { @Override protected void onCreate(Bundle savedInstanceStat

我看过一些关于片段的教程,其中创建了一个主活动,并使用按钮逐个更改片段。我的问题是,我可以使用被调用布局的按钮而不是主活动的按钮来更改片段吗。这就是我目前的困境。以前,我的应用程序运行时使用意图更改活动。但我遇到了一个问题,我需要使用片段来解决它

这是我的“主要”片段代码:

public class FragmentMain extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_main);
        Log.d("Batelec", "fragment main started");
    }

    public void selectFrag(View view) {
         Fragment fr;

         fr = new FragmentMain2();
         FragmentManager fm = getFragmentManager();
         FragmentTransaction fragmentTransaction = fm.beginTransaction();
         fragmentTransaction.replace(R.id.fragment1, fr);
         fragmentTransaction.commit();

    }
}
这是我的布局文件:

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mypower_build101.FragmentMain" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.mypower_build101.FragmentMain2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

下面是我的主要活动xml:

<RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/btnAddDevice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="@string/txtAddDevice"
        android:onClick="addClick" />

    <Button
        android:id="@+id/btnShowDevice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnAddDevice"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:text="@string/txtShowDevice"
        android:onClick="showClick" />

    <Button
        android:id="@+id/btnMainRegister"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnShowDevice"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="@string/txtRegister"
        android:onClick="registerClick" />

    <Button
        android:id="@+id/btnShowDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnMainRegister"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="29dp"
        android:text="@string/txtShowDialog" />

    <TextView
        android:id="@+id/txtStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnShowDialog"
        android:layout_centerHorizontal="true"
        android:layout_margin="@dimen/listViewPadding"
        android:layout_marginTop="20dp"
        android:text="@string/txtNull"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Switch
        android:id="@+id/switchGCM"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
         android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:checked="false"
        android:text="@string/txtConnect2GCM"
        android:textOff="@string/txtNo"
        android:textOn="@string/txtYes" />

    <TextView
        android:id="@+id/txtBroadCastMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lblStatus"
        android:layout_alignStart="@+id/lblStatus"
        android:layout_below="@+id/lblStatus"
        android:layout_marginTop="17dp"
        android:gravity="center"
        android:text="@string/txtNull"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

如果我真的需要在我已经运行的活动之外创建按钮,你能给我一些关于如何正确编码它们的提示吗?我已经看到使用静态不是最好的方法。另外,请给我一些关于如何在片段中放置新按钮的提示,因为我的主要活动有4个按钮,它们已经用于更改活动。
非常感谢所有帮助

好吧,您要做的是最好使用以编程方式添加到活动中的片段

我将留下一个关于活动的小项目,使用两个按钮,一个片段屏幕替换另一个片段屏幕

你可以在发送Gitub的链接中找到完整的代码,如果你想下载,你可以免费使用和分析

我留下了截图,这样你就可以看到它是如何工作的


我希望我的帮助非常有用。

你的意思是,我仍然需要一个单独的按钮来更改活动,我不能再使用以前的按钮了。我只是想,我可以使用我以前的主要活动,然后调用一个包含片段的活动吗。当我单击主活动的按钮时,该按钮调用“碎片化”活动并更改它调用的片段


嘿,我试着根据我上传的图片做我想做的事。所发生的是,碎片化活动显示在主活动的顶部。碎片似乎是透明的活动。你能教我如何隐藏主要活动吗。此外,片段中显示的活动上的可单击项也失去了其功能,例如“我的微调器”无法再将文本放在编辑文本上。