2016-08-13 73 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... 
}