如何在父活动类(Android)中获取子活动视图?

时间:2013-12-16 09:11:01

标签: android

我实现了一个底部按钮栏(类似于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中实现所选功能?

2 个答案:

答案 0 :(得分:0)

您可以使用BaseActivity中的booloean保持Button的状态(单击与否),并在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();
    }
}

答案 1 :(得分:0)

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

因此,您在参数中有一个直接指向btnAddParty按钮的视图实例。 这是您可以使用findViewByIdbtnAddParty ID而不是btnAddGuest ID进行访问的原因。

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

 View btAddParty = findViewById(R.id.btnAddParty)

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

现在,您可以通过在setMyInternalContent中实现一种特殊方法(下面的示例中为BaseActivity)并保留每个按钮的实例来获得完整的解决方案,之后,您只需要更新有关于点击操作的状态:

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