0

我喜欢制作带有标签的应用程序。我的应用程序中有三个选项卡,每个选项卡中有一些按钮。还有三个片段。当按下每个选项卡中的按钮时,我喜欢在通用 TextView 中显示一些文本。我的选项卡工作正常。但我有一些三个问题。1.如何为所有选项卡(可能在选项卡之外)提供一个通用的 TextView 2.我在片段中的按钮方法不起作用 3.当我尝试显示自定义语言字体时,它给了我一个错误“方法 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

    }

}}

这是我在其中一个片段中的代码

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

最后我得到了解决方案。我们可以在 Main 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 方法,可以直接从 Main Activity 调用它。Tab1.XMl、Tab2.xml等中的Button名称应该不同。代码如下,

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

}

方法 butn1 对应于 Tab1 中的 Button1,同样我们可以为其他选项卡中的其他按钮赋予。

于 2013-02-16T18:18:21.237 回答