0

タブ付きのアプリケーションを作るのが好きです。私のアプリケーションには3つのタブがあり、各タブにいくつかのボタンがあります。そして、3つのフラグメントもあります。各タブのボタンが押されたときに、共通のTextViewにテキストを表示するのが好きです。タブは正常に機能しています。しかし、私にはいくつかの3つの問題があります。1.すべてのタブ(おそらくタブの外側)に共通のTextViewを与える方法2.フラグメント内のボタンメソッドが機能しない3.cutom言語フォントを表示しようとすると、「メソッドgetAssets()はタイプFragmentAに対して未定義"

以下は私の主な活動です

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

    }

}}

これはフラグメントの1つにある私のコードです

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("അ");            

}}

どんな助けでもあなたに非常に感謝します。

4

1 に答える 1

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>

現在、フラグメントから呼び出すためにボタンは必要ありません。FindVuewById メソッドを指定することで、メイン アクティビティから直接呼び出すことができます。ボタン名は、Tab1.XMl、Tab2.xml などで異なる必要があります。コードは次のとおりです。

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

}

メソッド butn1 は、Tab1 の Button1 に対応します。同様に、他のタブの他のボタンにも適用できます。

于 2013-02-16T18:18:21.237 に答える