如何在android中使用带有页面指示器活动的按钮

时间:2014-09-11 11:15:00

标签: java android

我使用一个片段并使用三个图像作为页面滑块。当我使用此代码(按钮在第3屏幕上可见)时,指示器未移动或仅运行图像被切片。那么,我如何一起使用按钮和指示器。

public class MainActivity extends FragmentActivity {

TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

    mPager = (ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);
    Button button = (Button) findViewById(R.id.nextbutton1);

    CirclePageIndicator indicator = (CirclePageIndicator)findViewById(R.id.indicator);
    mIndicator = indicator;
    indicator.setViewPager(mPager);

    float density = getResources().getDisplayMetrics().density;
    indicator.setBackgroundColor(0xFFFFFFFF);
    indicator.setRadius(7 * density);
    indicator.setPageColor(0xFFFFFFFF);
    indicator.setFillColor(0xFFE63F36);
    indicator.setStrokeColor(0xFF000000);
    indicator.setStrokeWidth((float) (0.5* density));

    mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            Button button = (Button) findViewById(R.id.nextbutton1);
            if(position==2) {
                button.setVisibility(View.VISIBLE);

                button.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        // Do something in response to button click
                        Intent i=new Intent(MainActivity.this,Login.class);
                        startActivity(i);                       
                    }                      
                });  
            } else {
                button.setVisibility(View.GONE);
            }

        /* FragmentChangeListener listener = (FragmentChangeListener) //cast fragment at this position to FragmentChangeListener
        listener.onCentered();*/

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }
    });
}

0 个答案:

没有答案