2

ここでは 4 セットのボタンとそのRelativeLayout下に空があります。これらのいずれかをクリックすると、RelativeLayout別の 2 セットのボタン、つまりONとを含む空に別のレイアウトがポップアウトしますOFF。私の問題は、ボタンをクリックするたびに表示され、ボタンをクリックするたびに消えるようにするため、ボタンONOFFボタンで発生します。ここに私のレイアウトとコードがあります:ImageViewONOFF

これは、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>

そして、これは、これらの 4 つのうちの 1 つをクリックすると表示されるレイアウトの 1 つのコードです (同じボタンが表示されますが、表示する画像が異なります)。

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

私はそれを働かせることができません。最初に 4 つのボタンのいずれかをクリックしてからクリックONするOFFと、問題ないように見えますが、別のボタンをクリックすると、画像が表示されなくなります。

4

1 に答える 1

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メソッドは子レイアウトを追加します。そこにあるものを置き換えるものではなく、それらはすべて親レイアウトの子として階層内にあります。また、レイアウトで奇妙なことが起こっている場合は、デバッガーでそれを見て、そのプロパティを調べてください。この場合、オンとオフを繰り返し押すと、子のリストが拡大して表示されます。

于 2013-03-04T06:18:48.187 に答える