gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:06:19 25 4
gpt4 key购买 nike

我有一个简单的滑动页面示例,只有三页。
三个页面“黄色按钮”、“蓝色按钮”、“红色按钮”各有一个按钮。
我想将“黄色按钮”等按钮的名称更改为“+”,我该怎么做?
我尝试在“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com