0

我喜歡用選項卡製作應用程序。我的應用程序中有三個選項卡,每個選項卡中都有一些按鈕。還有三個片段。我喜歡在按下每個選項卡中的按鈕時將一些文本顯示到常用的TextView中。我的選項卡工作正常。但我有三個問題。 1.如何爲所有選項卡提供共同的TextView(可能在選項卡外) 2.我的按鈕方法中的片段不工作 3.當我嘗試顯示cutom語言字體時,它給我一個錯誤「The method getAssets ()是未定義的類型FragmentA」如何在操作欄選項卡外添加TextView

以下是我的主要活動

public class MLkeyboardActivity extends Activity { 
/** Called when the activity is first created. */ 
//public static TextView textView1; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_mlkeyboard); 


    final ActionBar actionBar = getActionBar();   
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 




    Tab tabA = actionBar.newTab(); 
    tabA.setText("Tab A"); 
    tabA.setTabListener(new TabListener<FragmentA>(this, "Tag A", FragmentA.class)); 
    tabA.setIcon(R.drawable.ic_launcher); 
    actionBar.addTab(tabA); 

    Tab tabB = actionBar.newTab(); 
    tabB.setText("Tab B"); 
    tabB.setTabListener(new TabListener<FragmentB>(this, "Tag B", FragmentB.class)); 
    actionBar.addTab(tabB); 

    Tab tabC = actionBar.newTab(); 
    tabC.setText("Tab C"); 
    tabC.setTabListener(new TabListener<FragmentC>(this, "Tag C", FragmentC.class)); 
    actionBar.addTab(tabC); 

    if (savedInstanceState != null) { 
     int savedIndex = savedInstanceState.getInt("SAVED_INDEX"); 
     getActionBar().setSelectedNavigationItem(savedIndex); 
    } 

} 

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    // TODO Auto-generated method stub 
    super.onSaveInstanceState(outState); 
    outState.putInt("SAVED_INDEX", getActionBar().getSelectedNavigationIndex()); 
} 

public static class TabListener<T extends Fragment> 
    implements ActionBar.TabListener{ 

    private final Activity myActivity; 
    private final String myTag; 
    private final Class<T> myClass; 

    public TabListener(Activity activity, String tag, Class<T> cls) { 
     myActivity = activity; 
     myTag = tag; 
     myClass = cls; 
    } 

    @Override 
    public void onTabSelected(Tab tab, FragmentTransaction ft) { 

     Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag); 

     // Check if the fragment is already initialized 
     if (myFragment == null) { 
      // If not, instantiate and add it to the activity 
      myFragment = Fragment.instantiate(myActivity, myClass.getName()); 
      ft.add(android.R.id.content, myFragment, myTag); 
     } else { 
      // If it exists, simply attach it in order to show it 
      ft.attach(myFragment); 
     } 

    } 

    @Override 
    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 

     Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag); 

     if (myFragment != null) { 
      // Detach the fragment, because another one is being attached 
      ft.detach(myFragment); 
     } 

    } 

    @Override 
    public void onTabReselected(Tab tab, FragmentTransaction ft) { 
     // TODO Auto-generated method stub 

    } 

}} 

,這是我的片段

public class FragmentA extends Fragment { 
public static TextView textView1; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false); 
    return myFragmentView;  
} 
public void button1(View view) { 
    TextView tv = (TextView) getActivity().findViewById(R.id.textView1); 
    Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/AnjaliOldLipi.ttf"); 
    tv.setTypeface(typeFace); 
    tv.setText("അ");    

}} 

我將極其比的一個碼向你求助任何幫助。

+0

爲什麼有人投票否決了我的問題?信息不是很清楚嗎?我真的需要幫助。請 – user1995307 2013-02-11 07:16:04

+0

感謝所有的投票ups。雖然我沒有得到任何答案。 – user1995307 2013-02-11 09:32:05

回答

0

最後我得到了解決方案。我們可以在主XML文件中提供一個常見的TextView,如下所示。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

<TextView 
android:id="@+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="TextView" />  

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

現在按鈕不需要從片段中調用ba。它可以通過給予FindVuewById方法直接從主要活動中調用。按鈕名稱應在Tab1.XMl不同,Tab2.xml等代碼如下,

public void butn1(View view) { 
    TextView tv = (TextView) findViewById(R.id.textView1);  
    tv.setText("abc"); 

} 

方法butn1對應於Button1的在TAB1,similerly我們可以給在其他標籤的其他按鈕。

相關問題