1

I am new to java, and I am still learning, can anyone help in this respect in their spare time?I am trying to do a 10 page poem, just to start with.

I have created buttons that look like a number pad, I am trying to make this number work when it has been pressed/clicked. I am able to make the numbers show when pressed. However, where I am having problems is how to make this number open another layout I have created with a number.

Say I have created a layout and I want it to be page 7, how do I set it such that when number 7 is pressed (it shows the first line / title of the poem) and when the "Go" button is clicked, it opens the layout with poem number 7.

Thank you

EDIT

I followed what you told me and now I have one layout and one fragment class.

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

<android.support.v4.view.ViewPager
    android:id="@+id/text_fragment_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<TextView
    android:id="@+id/tvpoem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />

`

and the fragment java class is like:
public class PoemMapFrag extends Fragment

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

    View view = inflater.inflate(R.layout.fragpoemsmap, container, false);
    TextView fragpageone = (TextView) view.findViewById(R.id.tvpoems);
    fragpageone.setText("Testing");
    return view;

}

public View onCreateView1(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragpoemsmap, container, false);
    TextView fragpagetwo = (TextView) view.findViewById(R.id.tvpoems);
    fragpagetwo.setText("Yawning");
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
}`
4

2 回答 2

1

处理按钮点击的方法是为其分配一个 OnClickListener,如下所示:

button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        //start a new activity or replace the Fragment in view
    }
};

对于您的特定情况,您可以使用Fragments显示每个页面,使用FragmentTransaction 替换 Fragment in view。如果您使用 Activity,您将有 10 个 OnCLickListeners(每页一个),所以我不建议这样做。

或者,您可以只使用滑动手势来切换 Fragment,从而摆脱您的按钮。ViewPager就是为此而设计的,您可以使用Jake Wharton 的 ViewPagerIndicator库来提供查看哪个页面的视觉反馈。

如果您需要我澄清某些事情,请不要犹豫;-)

编辑:根据您的要求,这是我使用片段的方法:

为每个页面创建一个片段。您每次都可以重复使用相同的布局,只包含一个带有页面文本的 TextView,宽度和高度设置为 match_parent。在片段的 onCreateView 中,您可以像这样获得对 textview 的引用:

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

    View view = inflater.inflate(R.layout.fragment_page_layout, container, false);

    // Get references to layout elements
    pageTextView = (TextView) view.findViewById(R.id.article_paragraph_title);
    return view;
}

在 onActivityCreated 中:

pageTextView.setText(yourTextForThisPage);

在您的活动中,您可以设置一个按钮来切换页面,或者使用 ViewPager。主要思想是 Activity 将负责切换 Fragment,从而更改视图中的文本,因为每次更改 Fragment 时,都应该创建一个新的 Fragment 来替换之前的 Fragment。您可以使用我上面提供的链接来帮助您入门,或者按照THIS TUTORIAL,这似乎或多或少地实现了我试图解释的内容。我希望这能澄清一点......

于 2013-10-25T14:00:27.157 回答
0
Button go = (Button) findViewById(R.id.BUTTON_ID);
go.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, Poem_7.class)
        startActivity(intent)
    }
});
于 2013-10-25T13:55:58.040 回答