简体   繁体   中英

How can I access to a view programmatically that declare invisible in xml?

I have a layout like this, main_layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout 
            android:id="@+id/testLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="64dp"
            >
            <ImageButton 
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="50dp"/>
            <ImageButton 
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="100dp"
                android:visibility="invisible"/>
</RelativeLayout>
</RelativeLayout>

Now I want to acces the second button in java code, so this is the code section that cause NullPointerException:

RelativeLayout rl = (RelativeLayout) findViewById(R.id.testLayout);
ImageButton b = (ImageButton) rl.getChildAt(1);

I have found a new interesting point.this code shows that rl has only one child in code section, so rl does not have the second button at all.why is that?

 log.d("child Counter", String.valueOf(rl.getChildCount())); 

this shows 1 in LogCat

And this is the onCreate method for those who think this is the cause of the problem, but I know this is not.

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
}

Step 1: Declare an id for the Second button as shown below and access it.

<RelativeLayout 
            android:id="@+id/testLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="64dp"
            >
            <ImageButton 
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="50dp"/>
            <ImageButton 
                android:id="@+id/imgButton2"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="100dp"
                android:visibility="invisible"/>
</RelativeLayout> 

and access it in java file

ImageButton b = (ImageButton)  findViewById(R.id.imgButton2);

Step 2: If you want to use getChild , please determine the whether you are able to retrive all the childs , In your case the Relative Layout has two child , this is can be achiveable using get rl.getChildCount(); and getChild and assign it to view as shown below.

for(int i=0;i<r1.getChildCount();i++){
       View child=relativeLayout.getChildAt(i);
       //your processing code....
  }

you might want to add an id for every widget

<RelativeLayout 
            android:id="@+id/testLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="64dp"
            >
            <ImageButton 
            android:id="@+id/imageview1"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="50dp"/>
            <ImageButton 
            android:id="@+id/imageview2"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="100dp"
                android:visibility="invisible"/>
</RelativeLayout>

and access it with

ImageButton b = (ImageButton) findViewById(R.id.imageview1);

then you can make it visible by

b.setVisiblity(View.VISIBLE);

Your code is working perfectly,try this way,hope this will help you to solve your problem.

activity_main.xml

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/testLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="64dp">
    <ImageButton
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="50dp"/>
    <ImageButton
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="100dp"
        android:visibility="invisible"/>
</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.testLayout);
        ImageButton b1 = (ImageButton) rl.getChildAt(0);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"Image Button 1 clicked",Toast.LENGTH_SHORT).show();
            }
        });
        ImageButton b2 = (ImageButton) rl.getChildAt(1);
        b2.setVisibility(View.VISIBLE);
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"Image Button 2 clicked",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM