在水平滑动视图分页中将SetText设置为Button

时间:2013-08-03 20:54:58

标签: android swing button horizontalscrollview

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

Customviewpager

  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>

2 个答案:

答案 0 :(得分:4)

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

 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

        }
    });

答案 1 :(得分: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中相应的字符串资源。