2016-08-13 72 views
0

我試圖從包含的佈局訪問TextView,但我不知道如何。 這是佈局我setContentView(R.layout.activity_home);onCreate從不同的xml文件訪問多個包含的佈局

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <include 
     layout="@layout/app_bar_home" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 

     app:menu="@menu/activity_home_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

你可以看到,這個佈局包括另一個佈局,app_bar_home.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 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" 
    android:fitsSystemWindows="true" 
    tools:context="com.example.branco.sauce.home"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 
</android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_home" 
     android:id="@+id/includeContentHome"/> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

現在這種其他佈局包括我最後的XML文件中,我有一個TextView,content_home.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.branco.sauce.home" 
    tools:showIn="@layout/app_bar_home" 

    > 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ROLA 3" 
     android:textSize="100px" 
     android:id="@+id/txtRola3" 
     /> 
    <ListView 
     android:id="@+id/listview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 

     /> 
</RelativeLayout> 

我想要的就是訪問TextView的哪個ID是txtRola3在下面的方法:

@Override 
     protected void onPostExecute(Void result) { 

      final LayoutInflater inflateContentHome = getLayoutInflater(); 
      final View viewContentHome = inflateContentHome.inflate(R.layout.app_bar_home, null); 
      TextView txtTest = (TextView)viewContentHome.findViewById(R.id.includeContentHome).findViewById(R.id.txtRola3); 
      txtTest.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        System.out.println("You clicked on the textview"); 
       } 
      }); 
} 

正如你所看到的,我已經嘗試了一些充氣的佈局,但我不知道如何使這項工作。我的意思是,我可以看到TextView和它的文本,但listener不起作用。我正在做這個,所以我可以學習和訪問我的ListView(現在我無法填充數據)。我知道這種問題之前已經得到解答,但由於我有三種佈局,一種包含另一種佈局,所以我無法用其他答案獲得運氣。

編輯:home.java:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 
     new RemoteDataTask().execute(); 
    } 
private class RemoteDataTask extends AsyncTask<Void, Void, Void> { 

     protected void onPostExecute(Void result) { 
      final LayoutInflater inflateContentHome = getLayoutInflater(); 
      final View viewContentHome = inflateContentHome.inflate(R.layout.app_bar_home, null); 
      TextView txtTest = (TextView)viewContentHome.findViewById(R.id.includeContentHome).findViewById(R.id.txtRola3); 
      txtTest.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        System.out.println("You clicked on the textview"); 
       } 
      }); 
} 
} 

OBS:我的代碼有大約800線,所以我說在這裏什麼最重要。

+0

可以使用添加不同的XML – KrishnaJ

+0

我不明白你的意思,@KrishnaJ –

+0

<包括佈局=「@佈局/ app_bar_home」 />你沒有 KrishnaJ

回答

0

事實證明,假設所有三種佈局都「連接」,我不需要創建充氣器。我所做的只是findViewByID,好像我的textview在主佈局上。

protected void onPostExecute(Void result) { 
       TextView k = (TextView)findViewById(R.id.rola3); 
       k.setOnClickListeer... 
}