2017-10-20 32 views
0

我試圖建立與顯示TableLayout1和當按下img1同時hidding TableLayout2的應用程序,但IM仍然得到錯誤...如何顯示TableLayout和其他隱藏在TableLayout 1的onClick

這是我的java代碼:

public class Info extends ActionBarActivity { 
ImageView img1; 
TableLayout tl, tl2; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_info); 
     tl = (TableLayout)findViewById(R.id.infolayout); 
     tl2 = (TableLayout)findViewById(R.id.trueident); 
     img1 = (ImageView)findViewById(R.id.imageView1); 


     img1.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       tl.setVisibility(View.INVISIBLE); 
       tl2.setVisibility(View.VISIBLE); 

      } 
     }); 

    } 


} 

所以問題是,我想表明trueident佈局誰被隱藏,隱藏infolayout誰自年初已經顯現。

這可能嗎?另外,當img1已經按下時,我應該如何編寫if的代碼來做相反的事情?

感謝您的提前!

回答

0

所有的作品都適合我。檢查你的文件並與此進行比較。此處還提供了什麼「說」您的Android監視器。

我的代碼:

package com.example.android.myapplication; 

import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.TableLayout; 

public class Info extends ActionBarActivity { 
    ImageView img1; 
    TableLayout tl, tl2; 

    @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    tl = (TableLayout)findViewById(R.id.infolayout); 
    tl2 = (TableLayout)findViewById(R.id.trueident); 
    img1 = (ImageView)findViewById(R.id.imageView1); 

    img1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 

      tl.setVisibility(View.INVISIBLE); 
      tl2.setVisibility(View.VISIBLE); 

     } 
    }); 

} 


} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.android.myapplication.Info" 
android:id="@+id/activity_info" 
> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="264dp" 
    app:srcCompat="@mipmap/ic_launcher" 
    tools:layout_editor_absoluteX="130dp" 
    tools:layout_editor_absoluteY="135dp" /> 

<TableLayout 
    android:id="@+id/infolayout" 
    android:layout_width="193dp" 
    android:layout_height="107dp" 
    android:background="@color/colorAccent"> 
</TableLayout> 

<TableLayout 
    android:id="@+id/trueident" 
    android:layout_width="match_parent" 
    android:layout_height="103dp" 
    android:background="@color/colorPrimary" 
    > 
</TableLayout> 

</TableLayout> 

的manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.android.myapplication"> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".Info"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application>