片段和活动

时间:2015-08-10 21:28:40

标签: android android-fragments fragment

您好我正在使用Tab Layout在Android中创建应用程序。我不熟悉片段,我不能让它工作,应用程序显示正确的布局,但按钮和类似的东西没有响应。这是我的代码(我不能创建第一堂课" FragmentActivity"因为调用者不会接受

public class ebookC extends android.support.v4.app.Fragment {



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


        return inflater.inflate(R.layout.layout_ebook, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


    }


    public static class ebookr extends FragmentActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_ebook);
            Button button= (Button) findViewById(R.id.nextbt);
            final TextView textView = (TextView) findViewById(R.id.pageCount);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    textView.setText("Works????");
                }
            });

        }
    }
}

1 个答案:

答案 0 :(得分:0)

我猜测活动ebookr实际上并未使用。我也猜测片段确实被使用了,但是你希望OnClickListener能够工作 以下是我假设你要做的事情:

public class ebookC extends android.support.v4.app.Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.layout_ebook, container, false);
        Button button= (Button) v.findViewById(R.id.nextbt);
        final TextView textView = (TextView) v.findViewById(R.id.pageCount);
        button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    textView.setText("Works????");
                }
            });
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
}