基于活动(即可见)片段以编程方式启用或禁用按钮

时间:2013-11-18 09:31:30

标签: android android-fragments button fragment

我对Android编程比较陌生,我真的需要帮助解决这个问题。我有一个围绕三个基本活动的应用程序。在一项活动中 - 迄今为止最大的活动 - 我在UI中有十个按钮,它们以逻辑(即逐步)方式实现16个不同的片段(每个片段具有自己的UI),具体取决于用户想要完成的内容。

因此,对于这16个不同片段中的每一个,我需要激活和停用(启用和禁用)UI中的各种按钮,具体取决于特定片段处于活动状态时允许用户执行的操作(即,在查看堆栈或在视图中或对用户可见)。实际上,每次将新片段加载到片段占位符/容器中时,我需要更改(即设置)所有10个按钮的状态(即启用状态),以便用户清楚地了解它们在此逻辑进程中的位置的步骤。

请注意:所有10个按钮始终可见(即应该是可见的),并且必须仅启用或禁用,具体取决于当前加载或当前在视图中的片段(即显示/对用户可见) )。好的,让我们看看一些代码......

以上是我在上面提到的活动“DmpAct.java”(完整的代码)......

package com.carzy.carzyapp;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DmpAct extends Activity implements OnClickListener {

Fragment fragment;
Fragment newFragment;
FragmentManager fragMan;
Button birtListBtn, evenListBtn, appoListBtn, todoListBtn, specListBtn, dmpExitBtn;

@SuppressLint({ "NewApi", "CommitTransaction" })
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    // Hide the Title Bar of the Application --> Must come before setting the Layout...
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    // Hide the Status Bar of Android OS --> Can also be done later...
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);


    // Now you can draw the second Layout --> HomeScreen of the Application...
    setContentView(R.layout.dmp_act);


    // Instantiate the FragmentManager and FragmentTranstion...
    FragmentManager fragMan = getFragmentManager();
    FragmentTransaction fragTrans = fragMan.beginTransaction();


    // Now you need to define or set the initial/start fragment to be loaded when the view is laid out...
    DMPWelcFrag startFragment = new DMPWelcFrag();
    fragTrans.add(R.id.dmpFragContainer, startFragment);
    fragTrans.commit();


    // Instantiate (or get references to) all buttons laid out in this Activity
    Button birtListBtn = (Button) findViewById(R.id.dmp_bir_btn);
    birtListBtn.setOnClickListener(this);

    Button evenListBtn = (Button) findViewById(R.id.dmp_eve_btn);
    evenListBtn.setOnClickListener(this);

    Button appoListBtn = (Button) findViewById(R.id.dmp_app_btn);
    appoListBtn.setOnClickListener(this);

    Button todoListBtn = (Button) findViewById(R.id.dmp_tod_btn);
    todoListBtn.setOnClickListener(this);

    Button specListBtn = (Button) findViewById(R.id.dmp_spe_btn);
    specListBtn.setOnClickListener(this);

    Button dmpExitBtn = (Button) findViewById(R.id.dmp_exi_btn);
    dmpExitBtn.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.dmp, menu);
    return true;
}

@SuppressLint("NewApi")
@Override
public void onClick(View v) {

    // Fragment newFragment;

    // Set the DMP Fragment state here and pass it on to the FragmentTransaction module that follows..
    if (v.getId() == R.id.dmp_bir_btn) {
        newFragment = new BirtListFrag();
    }
    else if (v.getId() == R.id.dmp_eve_btn) {
        newFragment = new EvenListFrag();
    }
    else if (v.getId() == R.id.dmp_app_btn) {
        newFragment = new AppoListFrag();
    }
    else if (v.getId() == R.id.dmp_tod_btn) {
        newFragment = new TodoListFrag();
    }
    else if (v.getId() == R.id.dmp_spe_btn) {
        newFragment = new SpecListFrag();
    }
    else {
        newFragment = new DMPWelcFrag();
    }


    if (v.getId() == R.id.dmp_exi_btn) {
        Intent go2Main = new Intent(DmpAct.this, MainAct.class);                  
        startActivity(go2Main);
    }
    else;


    FragmentTransaction transact = getFragmentManager().beginTransaction();
    transact.replace(R.id.dmpFragContainer, newFragment, "activeFrag");
    transact.addToBackStack("activeFrag");
    primeRelativeBtns();
    transact.commit();

};

@SuppressLint("NewApi")
public void primeRelativeBtns() {
    if (newFragment.equals("dmpBirtListFragTag")) {
        birtListBtn.setEnabled(false); 
        evenListBtn.setEnabled(true);
        appoListBtn.setEnabled(true);
        todoListBtn.setEnabled(true);
        specListBtn.setEnabled(true);
    } 
    else if (newFragment.getTag() == "dmpEvenListFragTag") {
        birtListBtn.setEnabled(true); 
        evenListBtn.setEnabled(false);
        appoListBtn.setEnabled(true);
        todoListBtn.setEnabled(true);
        specListBtn.setEnabled(true);
    } else;
}

}

请注意......在提交此问题时,我只完成了此活动使用的10个按钮中的6个(在上面的代码中很明显)...

无论如何......我们去......

以下是“dmp_act.xml”文件为此活动实现的UI ...

<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"
android:orientation="horizontal"
android:baselineAligned="false"
tools:context=".DmpAct">

<LinearLayout
    android:id="@+id/leftButtonColumn"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.2705"
    android:gravity="center"
    android:paddingTop="5pt"
    android:paddingBottom="10pt"
    android:paddingLeft="8pt"
    android:paddingRight="8pt"
    android:orientation="vertical">

    <ImageView 
        android:id="@+id/dmp_cat_sign"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.0180"
        android:contentDescription="@string/dmp_cat_sign"/>

    <Button
        android:id="@+id/dmp_bir_btn"
        android:layout_width="wrap_content"
        android:layout_height="23pt"
        android:layout_weight="0.0164"
        android:layout_marginBottom="13pt"/>

    <Button
        android:id="@+id/dmp_eve_btn"
        android:layout_width="wrap_content"
        android:layout_height="23pt"
        android:layout_weight="0.0164"
        android:layout_marginBottom="13pt"/>

    <Button
        android:id="@+id/dmp_app_btn"
        android:layout_width="wrap_content"
        android:layout_height="23pt"
        android:layout_weight="0.0164"
        android:layout_marginBottom="13pt"/>

    <Button
        android:id="@+id/dmp_tod_btn"
        android:layout_width="wrap_content"
        android:layout_height="23pt"
        android:layout_weight="0.0164"
        android:layout_marginBottom="13pt"/>

    <Button
        android:id="@+id/dmp_spe_btn"
        android:layout_width="wrap_content"
        android:layout_height="23pt"
        android:layout_weight="0.0164"/>
</LinearLayout>

<LinearLayout
    android:id="@+id/dmpFragContainer"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.5350"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingTop="8pt"
    android:paddingBottom="8pt">

    <!-- <ImageView
        android:id="@+id/dmp_wel_sta_wal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:alpha="1"
        android:contentDescription="@string/dmp_welc_wall"/> -->

</LinearLayout>

<LinearLayout
    android:id="@+id/rightButtonColumn"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.1795"
    android:gravity="center"
    android:paddingTop="20pt"
    android:paddingBottom="20pt"
    android:paddingLeft="8pt"
    android:paddingRight="8pt"
    android:orientation="vertical">

    <Button
        android:id="@+id/dmp_edi_btn"
        android:layout_width="wrap_content"
        android:layout_height="0pt"
        android:layout_weight="1"
        android:layout_marginBottom="15pt"/>

    <Button
        android:id="@+id/dmp_sav_btn"
        android:layout_width="wrap_content"
        android:layout_height="0pt"
        android:layout_weight="1"
        android:layout_marginBottom="15pt"/>

    <Button
        android:id="@+id/dmp_add_btn"
        android:layout_width="wrap_content"
        android:layout_height="0pt"
        android:layout_weight="1"
        android:layout_marginBottom="15pt"/>

    <Button
        android:id="@+id/dmp_del_btn"
        android:layout_width="wrap_content"
        android:layout_height="0pt"
        android:layout_weight="1"
        android:layout_marginBottom="15pt"/>

    <Button
        android:id="@+id/dmp_exi_btn"
        android:layout_width="wrap_content"
        android:layout_height="0pt"
        android:layout_weight="1"/>
</LinearLayout>

在发布之前,我尝试了所有已经在SO中讨论的可用解决方案......但无济于事。所以总结一下这个问题......每当在“dmpFragContainer”中向用户加载(和显示)一个新片段时,我基本上需要一种非常好的方法(最佳实践)来设置或更改所有按钮的启用状态。 / p>

如果看起来我正在和任何人说话,我想事先道歉,但我想确保阅读这篇文章的每个人都能清楚地理解手头的问题。如果您认为我可以实现更好的“最佳实践”代码结构,请随意粉碎我的代码 - 正如我之前所说的......我是Android编码的新手,需要我能得到的所有帮助。

感谢帮助...

干杯,

SilSur。

1 个答案:

答案 0 :(得分:2)

Thweeet!我解决了这个问题。大家好......我设法解决了这个问题,我启用或禁用了所有十个按钮,就像我希望每次加载新片段并且用户可见时禁用它们一样。解决方案是您必须在每个片段的类文件中实现此“启用/禁用”代码。实际上很简单......所以这里没有进一步的解决方案是一种更容易理解的解决方案[代码] ......

这是我在应用程序中运行的16个片段中只有一个“BirtListFrag.java”的代码......

package com.carzy.carzyapp;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

@SuppressLint("NewApi")
public class BirtListFrag extends Fragment {

public static Button BirCatBtn;
public static Button EveCatBtn;
public static Button AppCatBtn;
public static Button TodCatBtn;
public static Button SpeCatBtn;
public static Button EdiFunBtn;
public static Button SavFunBtn;
public static Button AddFunBtn;
public static Button DelFunBtn;
public static Button ExiFunBtn;

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

    return inflater.inflate(R.layout.dmp_birt_list_frag, container, false);

}

@Override 
public void onActivityCreated (Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);

    /*** Get references to all the buttons you want to manipulate everytime this Fragment is loaded & visible in the viewport ***/
    BirCatBtn = (Button) getActivity().findViewById(R.id.dmp_bir_btn);
    EveCatBtn = (Button) getActivity().findViewById(R.id.dmp_eve_btn);
    AppCatBtn = (Button) getActivity().findViewById(R.id.dmp_app_btn);
    TodCatBtn = (Button) getActivity().findViewById(R.id.dmp_tod_btn);
    SpeCatBtn = (Button) getActivity().findViewById(R.id.dmp_spe_btn);
    EdiFunBtn = (Button) getActivity().findViewById(R.id.dmp_edi_btn);
    SavFunBtn = (Button) getActivity().findViewById(R.id.dmp_sav_btn);
    AddFunBtn = (Button) getActivity().findViewById(R.id.dmp_add_btn);
    DelFunBtn = (Button) getActivity().findViewById(R.id.dmp_del_btn);
    ExiFunBtn = (Button) getActivity().findViewById(R.id.dmp_exi_btn);


    /*** Now you can manipulate whatever attributes (***See Below***) of the buttons u created references to ABOVE ***/
    BirCatBtn.setEnabled(false);
    EveCatBtn.setEnabled(true);
    AppCatBtn.setEnabled(true);
    TodCatBtn.setEnabled(true);
    SpeCatBtn.setEnabled(true);
    EdiFunBtn.setEnabled(false);
    SavFunBtn.setEnabled(false);
    AddFunBtn.setEnabled(true);
    DelFunBtn.setEnabled(false);
    ExiFunBtn.setEnabled(true);
     }
}

正如你所看到的......这是非常合乎逻辑的。无论如何。我将这个突破归功于“Greensy”,他回答了“ColorFrog”发布的关于我遇到的类似问题的问题==&gt;如果你想看看我在说什么,这就是跳跃......“Disabling buttons in a Fragment”。

Anywho ...在发布此回复时我曾是S.O.的成员。只有5天,因此我无法“赞成”格林西的回答,因为我当时没有这种特权。所以,既然他的回答确实帮助我解决了我的问题,我决定至少我能做的就是发布这个回复并感谢他/她。所以,谢谢你的配偶...我肯定欠你一些东西(嗯......可能是啤酒?!)。谁知道。无论如何,我希望有一天会得到回报。

干杯,

肖氏-T