【发布时间】:2013-12-01 09:32:54
【问题描述】:
我对 Android 编程比较陌生,我确实需要帮助解决我正在尝试解决的这个问题。我有一个围绕三个基本活动的应用程序。在一个活动中——迄今为止最大的一个活动——我在 UI 中有十个按钮,它们根据用户想要完成的任务以逻辑(即逐步)方式实现 16 个不同的片段(每个片段都有自己的 UI)。
因此,对于这 16 个不同的片段中的每一个,我需要根据特定片段处于活动状态时(即在查看堆栈或在视图中或对用户可见)。实际上,每次将新片段加载到片段占位符/容器中时,我都需要更改(即设置)所有 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”中向用户加载(并显示)新片段时。
如果我似乎在对任何人说话,我想事先道歉,但我想确保阅读这篇文章的每个人都能清楚地理解手头的问题。如果您认为我可以实现更好的“最佳实践”代码结构,请随时撕碎我的代码 - 正如我之前所说...我是 Android 编码新手,需要我能获得的所有帮助。
感谢您的帮助...
干杯,
SilSur.
【问题讨论】:
标签: android android-fragments button fragment