1

我有一个简单的滑动页面示例,只有三页。
“黄色按钮”、“蓝色按钮”、“红色按钮”三个页面各有一个按钮。
我想将“黄色按钮”等按钮的名称更改为“+”,我该怎么做?
我尝试在“onClickYellowButton”方法中进行更改,但直到用户按下一个键并且我想在显示页面后立即更改。
谢谢你的帮助。

自定义viewpager

  public class CustomPagerAdapter extends PagerAdapter {

public Object instantiateItem(View collection, int position) {

    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

      int resId = 0;
    switch (position) {
    case 0: {
        resId = R.layout.page_1;
        break;
    }
    case 1: {
        resId = R.layout.page_2;
        break;
    }
    case 2: {
        resId = R.layout.page_3;
        break;
    }
    }

    View view = inflater.inflate(resId, null);

    ((ViewPager) collection).addView(view, 0);

    return view;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);

}

@Override
public Parcelable saveState() {
    return null;
}

@Override
public int getCount() {
    return 3;
}
}

主要活动

public class MainActivity extends Activity {

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

    // Create and set adapter
    CustomPagerAdapter adapter = new CustomPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.customviewpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(1);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

/**
 * Click button on blue page
 */
public void onClickBlueButton(View v) {
    Toast.makeText(getApplicationContext(), "Blue screen", Toast.LENGTH_SHORT).show();
}

/**
 * Click button on yellow page
 */
public void onClickYellowButton(View v) {
 Button b=(Button) findViewById(R.id.buttonYellow);
    b.setText("+");

    Toast.makeText(getApplicationContext(), "Yellow screen", Toast.LENGTH_SHORT).show();
}

/**
 * Click button on red page
 */
public void onClickRedButton(View v) {
    Toast.makeText(getApplicationContext(), "Red screen",  Toast.LENGTH_SHORT).show();
}
}

主要的 xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

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

</LinearLayout>

第 1 页

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue"
android:orientation="vertical" >

<Button
    android:id="@+id/buttonBlue"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/label_blue"
    android:onClick="@string/listenerBlueButton" />

</LinearLayout>

第2页

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/yellow"
android:orientation="vertical" >

<Button
    android:id="@+id/buttonYellow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/label_yellow"
    android:onClick="@string/listenerYellowButton" />

</LinearLayout>

第 3 页

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
android:orientation="vertical" >

<Button
    android:id="@+id/buttonRed"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/label_red"
    android:onClick="@string/listenerRedButton" />

</LinearLayout>
4

2 回答 2

4

您可以通过设置“setOnPageChangeListener”事件来做到这一点。在 onPageSelected 事件中,您可以编写逻辑 btn.setText("+"); 在这里,但您需要通过一些普通逻辑找出当前可见的按钮。

 myPager.setOnPageChangeListener(new OnPageChangeListener() {

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

        }

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

        }

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

        }
    });
于 2013-08-03T21:31:02.367 回答
0

启动时每个按钮中显示的文本在您的 layout.xml 中定义,例如:

<Button
    android:id="@+id/buttonYellow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/label_yellow" // This is where you define the text
    android:onClick="@string/listenerYellowButton" />

如果您想更改文本,我建议您更改 strings.xml 中相应的字符串资源。

于 2013-08-03T21:23:29.433 回答