【问题标题】:Controlling an ImageView using a Button click使用按钮单击控制 ImageView
【发布时间】:2013-01-24 04:50:29
【问题描述】:

我在这里有四组按钮和一个空的RelativeLayout,每当我单击其中一个时,另一个布局将在空的RelativeLayout 中弹出,其中包含另外两组按钮,即 ON kbd> 和 OFF。我的问题出现在 ONOFF 按钮中,因为我希望在单击 ON 按钮时出现ImageView 并在单击时消失关闭按钮。这是我的布局和代码:

这是 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>

这是用ONOFF按钮调用布局时的功能:

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

我无法让它工作。当我第一次单击 4 个按钮中的一个,然后单击 ON / OFF 时,它看起来不错,但是,每当我单击另一个按钮时,图像就不再可见。

【问题讨论】:

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


    【解决方案1】:

    你的问题其实出在这部分:

    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);

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

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

    【讨论】:

      猜你喜欢
      • 2018-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 2018-02-06
      相关资源
      最近更新 更多