android - 在水平滑动 View 分页中将文本设置为按钮

标签 android swing button horizontalscrollview

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

自定义 View 页面

  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;
}
}

主要 Activity

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>

最佳答案

您可以通过设置“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

        }
    });

关于android - 在水平滑动 View 分页中将文本设置为按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18037234/

相关文章:

java - 如何在调整框架大小时自动居中 JFrame 中使用的组件?

javascript - 带有 Yes/No 选项的 JS 按钮提示

vba - 宏在每个 excel 表 vba 上工作

java - 按后退按钮可在 fragment 中查看

java - 智能手机:不支持java

安卓:SlidingMenu 和 ActionBarActivity

android - 按下后退按钮

java - 更新 Jtable 中的复选框进入递归,导致堆栈溢出错误

java - 为什么JFrame无法显示任何形状?

python - 你好!我在 tkinter 网格对齐和按钮大小方面遇到一些问题