2014-06-22 4 views
0

Je suis en train de créer une application avec des fragments et en fragments je veux ajouter des boutons. Voici le codeajouter des boutons en fragments android

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentTabHost; 

import com.example.lightcontrolsystem.R; 

public class MainActivity extends FragmentActivity { 
    private FragmentTabHost mTabHost; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 
     setTabContent(); 

    } 

    private void setTabContent() { 
     mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 
     mTabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout); 



     mTabHost.addTab(
       mTabHost.newTabSpec("Pattern list").setIndicator("Pattern List", 
         getResources().getDrawable(android.R.drawable.star_on)), 
       PatternListFragment.class, null); 

       } 
} 

Voici le fragment que je veux ajouter buutons:

public class PatternListFragment extends Fragment { 

Button Theme1, Theme2, Theme3; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View view=inflater.inflate(R.layout.patterns_list, container,false); 
    //Theme1 = (Button)findViewById(R.id.theme1); 


// Theme1.setOnClickListener(buttonTheme1tOnClickListener); 

    return view; 
} 
} 

J'essayé d'ajouter un buuton, mais je peux voir quelques erreurs dans findViewById (R.id.theme1)

`

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/theme1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="theme1" /> 

</LinearLayout> 

`

`<android.support.v4.app.FragmentTabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TabWidget 
      android:id="@android:id/tabhost" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0" 
      android:orientation="horizontal" /> 

     <FrameLayout 
      android:id="@+id/tabFrameLayout" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 

</android.support.v4.app.FragmentTabHost > 
` 

Comment puis-je ajouter des gestionnaires de boutons dans Fragment? c'est possible ?

Merci

Répondre

1

Dans le fragment vous gonflez une vue, vous devez donc obtenir tous les id avec la référence de cette vue. Ci-dessous est l'exemple de code.

public class PatternListFragment extends Fragment implements OnClickListener { 

Button Theme1, Theme2, Theme3; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 

View view=inflater.inflate(R.layout.patterns_list, container,false); 
Theme1 = (Button)view.findViewById(R.id.theme1); 
Theme1.SetOnClickListener(this); 

return view; 
} 
@Override 
public void onClick(View v) { 

    switch (v.getId()) { 
    case R.id.Theme1: 

     break; 
// same way for other buttons 
    default: 
     break; 
    } 
    } 

} 
+0

@Raghunandan Merci, ci-dessus solution travaillé. Maintenant, j'ai un autre problème My Tab/Fragment et Button se chevauchent. Comment puis-je résoudre ce problème dans mon fichier .xml? – ashok449

+0

mon fichier main.xml ajouté ci-dessus – ashok449

+0

@ ashok449 posez une question différente pour un nouveau problème – Raghunandan

Questions connexes