使用按钮单击控制ImageView

时间:2013-01-24 04:50:29

标签: android android-layout android-intent android-imageview android-button

我这里有四组按钮和一个空的RelativeLayout,每当我点击其中一个时,另一个布局会弹出空RelativeLayout,其中包含另外两组按钮,即 ON OFF 。我的问题出现在 ON OFF 按钮中,因为每当我点击 ON 按钮时,我都希望显示ImageView&每当我点击 OFF 按钮时消失。这是我的布局和代码:

这是4个按钮的布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/green_bg"
    tools:context=".AleccMainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/please_choose"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#CAFFD8" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="fill_parent"
        android:layout_height="70dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:background="#95FF4F" >

        <Button
            android:id="@+id/btnAcu"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/tab_acu"
            android:onClick="gotoACU" />

        <Button
            android:id="@+id/btnFan"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/tab_fan"
            android:onClick="gotoFan" />

        <Button
            android:id="@+id/btnLight"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/tab_light"
            android:onClick="gotoLight" />

        <Button
            android:id="@+id/btnHallway"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@drawable/tab_hallway"
            android:onClick="gotoHallway" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/rl"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/linearLayout" >

    </RelativeLayout>

</RelativeLayout>

这是我点击其中一个时会出现的其中一个布局的代码(它们内部有相同的按钮,但显示不同的图像):

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/green_bg"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="16dp" >

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:onClick="offFAN"
            android:text="@string/off" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:onClick="onFAN"
            android:text="@string/on" />
    </LinearLayout>

        <ImageView
            android:id="@+id/fanOn"
            android:layout_below="@+id/linearLayout1"
            android:layout_alignParentLeft="true"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:contentDescription="@string/acu"
            android:visibility="invisible"
            android:src="@drawable/fan_on" />


</RelativeLayout>

这是使用 ON OFF 按钮调用布局时的功能:

public void gotoFan(View v){
    // Do something in response to button
    RelativeLayout rlFAN = (RelativeLayout) findViewById(R.id.rl);
    getLayoutInflater().inflate(R.layout.alecc_fan, rlFAN, true);
}

这就是我认为我的问题存在的地方:

public void offFAN (View view) {
    String messageToSend = "FANS OFF";
    SmsManager.getDefault().sendTextMessage(number, null, messageToSend, null,null);
    Toast.makeText(AleccMainActivity.this,"Electric Fans turned OFF", Toast.LENGTH_SHORT).show();

    ImageView fanON = (ImageView) findViewById(R.id.fanOn);
    fanON.setVisibility(View.GONE);     
}

public void onLIGHT (View view) {
    String messageToSend = "LIGHTS ON";
    SmsManager.getDefault().sendTextMessage(number, null, messageToSend, null,null);
    Toast.makeText(AleccMainActivity.this,"Flourescent Lights turned ON", Toast.LENGTH_SHORT).show();

    ImageView lightON = (ImageView) findViewById(R.id.lightOn);
    lightON.setVisibility(ImageView.VISIBLE);       
}

我无法让它发挥作用。当我第一次单击其中一个按钮然后单击 ON / OFF 时,它看起来很好,但每当我点击另一个时,图像就不再可见了。

1 个答案:

答案 0 :(得分:4)

您的问题实际上在此部分:

public void gotoFan(View v){
    // Do something in response to button
    RelativeLayout rlFAN = (RelativeLayout) findViewById(R.id.rl);
    getLayoutInflater().inflate(R.layout.alecc_fan, rlFAN, true);
}

每次打电话

getLayoutInflater().inflate(R.layout.alecc_fan, rlFAN, true);

它在RelativeLayout指定的布局中添加了另一个子R.id.rl。 这些布局中的每一个都有一个名为相同的ImageView(例如fanOn),因此当您尝试在fanOn上设置可见性时,它会将可见性转换为层次结构中的第一个子级,这将被所有随后的布局。

请记住:您的.inflate方法会添加子布局。它不会替换那里的内容,它们都在层次结构中作为父布局的子项。此外,如果布局发生奇怪的事情,请在调试器中查看它并检查其属性。在这种情况下,当你反复按下或关闭时,你会看到一个不断扩大的孩子列表。

相关问题