gpt4 book ai didi

android - 使用 Button 单击控制 ImageView

转载 作者:搜寻专家 更新时间:2023-11-01 08:05:46 24 4
gpt4 key购买 nike

我这里有四组按钮和一个空的 RelativeLayout 在它们下面,每当​​我点击其中一个时,另一个布局将在包含另外两个的空 RelativeLayout 中弹出一组按钮,即 ONOFF。我的问题出现在 ONOFF 按钮中,因为我希望每当我单击 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>

这个是用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 时,它看起来很好,但是,每当我点击另一个按钮时,图像就不再可见。

最佳答案

你的问题实际上是在这个部分:

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 方法添加了一个子布局。它不会替换那里的东西,它们都在层次结构中作为父布局的子级。此外,如果布局发生了奇怪的事情,请在调试器中查看它并检查其属性。在这种情况下,当您反复按下开关时,您会看到一个不断扩大的 child 列表。

关于android - 使用 Button 单击控制 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14494184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com