【问题标题】:How to get child Activity View in Parent Activity Class(Android)?如何在父活动类(Android)中获取子活动视图?
【发布时间】:2013-12-16 18:10:14
【问题描述】:

我实现了一个底部按钮栏(类似于 iPhone 中的标签栏控制器)。为此,我创建了一个通用布局(button_bar.xml,5 个图像按钮)并包含在其他活动 xml 文件中。为了管理点击操作,我创建了一个从 Activity 扩展的 BaseActivity.java 并执行点击操作。我从这个 BaseActivity 扩展了其他需要按钮栏的活动,效果很好。现在我想为这些按钮包含一个选定状态,但是当我访问基本活动中的按钮时,它会给出一个空指针错误。我该如何解决这个问题。

button_bar.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bottomButtonBar"
style="@android:style/ButtonBar"
... >
<ImageButton
     android:id="@+id/btnGuests"
    android:onClick="showAllGuests"
    android:src="@drawable/ic_guest_list" />

<ImageButton
    android:id="@+id/btnAddGuest"
    android:onClick="selectGuestType"
    android:src="@drawable/ic_add_guest" />

<ImageButton
    android:id="@+id/btnAddParty"
    android:onClick="showAddParty"
    android:src="@drawable/ic_add_party" />
    ....
</LinearLayout>

public class BaseActivity extends Activity {
    // common to other activities.
    public void showAddParty(View view) {
    //showAddParty is one one of the buttons click method. 4 more buttons are there
        Intent intent = new Intent(this, AddPartyActivity.class);
        startActivity(intent);

        // I can get "view.findViewById(R.id.btnAddParty)" here
        // but I can't get "findViewById(R.id.btnAddGuest)" here. how this possible
    }
}

public class AddPartyActivity extends BaseActivity{
    ....
}

这里我可以从参数视图中获取对应的视图,并更改backgroundIbageSource。但是当它转到从 Baseactivity 继承的“AddPartyActivity”时,新图像被旧图像替换。如何在 BaseActivity 本身中实现所选功能?

【问题讨论】:

  • 创建父活动的对象并从中访问
  • @PiyushGupta 制作一个活动对象???怎么样?

标签: android


【解决方案1】:

您可以在 BaseActivity 中使用 booloean 保持按钮的状态(单击或未单击),并在 AddPropertyActivity 的 onCreate() 方法中检查这些变量的值。

public class BaseActivity extends Activity {
    protected static boolean isButton1Clicked = false;
    protected static boolean isButton2Clicked = false;
    protected static boolean isButton3Clicked = false;
    protected static boolean isButton4Clicked = false;
    .....
    public void showAddParty(View view) {
       isButton1Clicked = true;
      //showAddParty is one one of the buttons click method. 4 more buttons are there
       Intent intent = new Intent(this, AddPartyActivity.class); 
       startActivity(intent);
    }
}


public class AddPartyActivity extends BaseActivity {

     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_layout);
         Button button1 = (Button)findViewById(R.id.button1);
         if(isButton1Clicked) {
             button1.setBackground(R.drawable.clicked);
         } else {
             button1.setBackground(R.drawable.not_clicked);
         }
      ......
      ......
      }
}

并记住所有布尔变量必须是static 变量。 (每个对象都有自己的实例变量副本,但所有对象都将共享一个静态成员副本)。

public class BaseActivity extends Activity {

    protected static boolean isButton1Clicked = false;
    protected static boolean isButton2Clicked = false;
    protected static boolean isButton3Clicked = false;
    protected static boolean isButton4Clicked = false;
    protected Button button1;
    protected Button button2;
    protected Button button3;
    protected Button button4;

    protected void checkButtonsState() {
        if (isButton1Clicked) {
            button1.setBackgroundResource(R.drawable.image1);
        } else {
            button1.setBackgroundResource(R.drawable.image2);
        }
       ..........
    }

    public void showAddParty(View view) {
        isButton1Clicked = true;
        // showAddParty is one one of the buttons click method. 4 more
        // buttons are there
        Intent intent = new Intent(this, AddPartyActivity.class);
        startActivity(intent);
    }
}

public class AddPartyActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button) findViewById(R.id.button);
        button2 = (Button) findViewById(R.id.item1);
          .....
        checkButtonsState();
    }
}

【讨论】:

  • 是的,这是一种解决方案,但问题是许多活动继承了基础活动,所以我需要在每个地方编写代码。这就是为什么我看起来可以在 BaseActivity 本身中做的事情。
  • @bolt123 如果所有活动布局具有相同的按钮和相同的 ID,您可以将初始化部分移动到基本活动。(在基本活动中编写一个方法,如 initializeButtons() 并在设置 contentView 后在 ChildActivity 中调用该方法
  • 感谢 Gopal Rao。它是一个普通的底部栏,有 5 个按钮。我在 Base 活动本身中做了初始化部分。 showAddParty() 也有许多子活动,并且选择也需要存在于该活动中。我使用全局状态进行了管理。标签栏现在工作正常。一个问题是需要在所有继承的活动(超过 15 个)中调用 checkButtonsState()。我们可以避免这种情况(在 BaseActivity 本身中)
  • @bolt123 覆盖 BaseActivity 中的 onStart() 方法并在其中调用 checkButtonsState() 。确保在 onStart() 之前实例化所有按钮
【解决方案2】:

您的错误来自因为您使用的是view.findViewById(),但视图变量对应于捕获点击事件的对象(查看您的xml,'android:onClick="showAddParty"')

因此,您在参数中有一个直接指向btnAddParty 按钮的视图实例。 这就是为什么您可以使用findViewById 访问btnAddParty id 而不是btnAddGuest id。

您可以通过使用直接 findViewById 作为活动类的方法来访问所有视图层次结构:

 View btAddParty = findViewById(R.id.btnAddParty)

 View btAddGuest = view.findViewById(R.id.btnAddGuest)

现在,您可以获得完整的解决方案,通过在BaseActivity 中实现一个特殊方法(下面示例中的setMyInternalContent)并保留每个按钮的实例,之后,您只需在点击时更新那里的状态行动:

public class BaseActivity extends Activity {
    protected Button mBtAddParty;
    protected Button mBtAddGuest;
    // ...

    protected void setMyContentView(int resId) {
        setContentView(resId);
        mBtAddParty = (Button)findViewById(R.id.btnAddParty);            
        mBtAddParty = (Button)findViewById(R.id.btnAddGuest);   
        // ...            
    }

    public void showAddParty(View view) {
        mBtAddParty.setClickable(true);
        mBtAddGuest.setClickable(false);
        // ...

        //showAddParty is one one of the buttons click method. 4 more buttons are there
        Intent intent = new Intent(this, AddPartyActivity.class);
        startActivity(intent);
    }
}


public class AddPartyActivity extends BaseActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setMyContentView(R.layout.activity_layout);

        // Do not forget to set initial button state:            
        mBtAddParty.setClickable(true);
        mBtAddGuest.setClickable(false);
        // ...
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 2014-04-04
    相关资源
    最近更新 更多