gpt4 book ai didi

java - 按钮仅在第二次触摸后起作用

转载 作者:行者123 更新时间:2023-11-30 00:12:17 24 4
gpt4 key购买 nike

我制作了一个幻灯片 Activity 并且有两个按钮。一个到达下一层,另一个到达上一层。但它们仅在触摸 2 次时起作用。当触摸一次时,它什么也不会发生。我希望它们在触摸 1 次时工作。像往常一样。

Activity :

package world.cryneo.partytime.java;

public class introActivity extends AppCompatActivity {

private ViewPager mSlideViewPager;
private LinearLayout mDotLayout;
private SlideAdapter slideAdapter;
private TextView[] mDots;
private Button mNextBtn, mBackBtn;
private int mCurrentPage;

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

mSlideViewPager = (ViewPager) findViewById(R.id.slideViewPager);
mDotLayout = (LinearLayout) findViewById(R.id.dots_layout);
mNextBtn = (Button) findViewById(R.id.button3);
mBackBtn = (Button) findViewById(R.id.button2);

slideAdapter = new SlideAdapter(this);

mSlideViewPager.setAdapter(slideAdapter);

addDotsIndicator(0);

mSlideViewPager.addOnPageChangeListener(viewListener);


mNextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if (mNextBtn.getText().equals("Fertig")){

Intent settingsIntent = new Intent(introActivity.this, MainActivity.class);
startActivity(settingsIntent);
}

mSlideViewPager.setCurrentItem(mCurrentPage++);
}
});

mBackBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

mSlideViewPager.setCurrentItem(mCurrentPage--);
}
});

}

public void addDotsIndicator(int position){

mDots = new TextView[4];
mDotLayout.removeAllViews();
for (int i=0; i< mDots.length; i++){

mDots[i] = new TextView(this);
mDots[i].setText(Html.fromHtml("&#8226;"));
mDots[i].setTextSize(35);
mDots[i].setTextColor(getResources().getColor(R.color.white));
mDotLayout.addView(mDots[i]);

}
if (mDots.length > 0){
mDots[position].setTextColor(getResources().getColor(R.color.colorwhite));
}
}

ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {

}

@Override
public void onPageSelected(int i) {

addDotsIndicator(i);
mCurrentPage = i;
if (i == 0) {

mBackBtn.setVisibility(View.INVISIBLE);
mNextBtn.setText("Weiter");
mBackBtn.setText("Zurück");
} else if (i == mDots.length -1){

mBackBtn.setVisibility(View.VISIBLE);
mNextBtn.setText("Fertig");
mBackBtn.setText("Zurück");
} else {

mBackBtn.setVisibility(View.VISIBLE);
mNextBtn.setText("Weiter");
mBackBtn.setText("Zurück");

}
}

@Override
public void onPageScrollStateChanged(int i) {

}
};
}

slider 适配器:

package world.cryneo.partytime.java;

public class SlideAdapter extends PagerAdapter {

Context context;
LayoutInflater layoutInflater;

public SlideAdapter (Context context){

this.context = context;

}

// Array
public int[] slide_images = {
R.drawable.event,
R.drawable.hype,
R.drawable.freunde,
R.drawable.profil,

};

public String[] slide_headings = {
"Events", "Vorteile","Freunde","Profil"
};

public String[] slide_descs = {
"Betrachte alle Events und klicke auf Sie um weitere Informationen zu erhalten.",
"Klicke auf die Events und schau dir Details an. Exklusive Vorteile warten auf dich! Wenn dir das Event gefällt wirst du 3 Stunden vor Beginn benachrichtigt steigerst du das Hype-o-Meter.",
"Finde Bekanntschaften und nimm Kontakt auf.",
"OKay hallo 4"
};


@Override
public int getCount() {
return slide_headings.length;
}

@Override
public boolean isViewFromObject(View view, Object o) {
return view == (RelativeLayout) o;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {

layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.slide_layout, container, false);

ImageView slideImageView = (ImageView) view.findViewById(R.id.slide_image);
TextView slideHeading = (TextView) view.findViewById(R.id.slide_heading);
TextView slideDescription = (TextView) view.findViewById(R.id.slide_desc);

slideImageView.setImageResource(slide_images[position]);
slideHeading.setText(slide_headings[position]);
slideDescription.setText(slide_descs[position]);

container.addView(view);

return view;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout) object);
}
}

感谢您的帮助。

最佳答案

改变

mSlideViewPager.setCurrentItem(mCurrentPage++);mSlideViewPager.setCurrentItem(++mCurrentPage);

mSlideViewPager.setCurrentItem(mCurrentPage--);mSlideViewPager.setCurrentItem(--mCurrentPage);

使用 mCurrentPage++ 调用 setCurrentItem(0) 然后才将 mCurrentPage 更改为 1。下次单击按钮时,它会调用 setCurrentItem(1) 并将 mCurrentPage 设置为 2。mSlideViewPager 从第 1 页开始。 0 所以 setCurrentItem(0) 什么都不做,只有第二次点击触发页面从 0 到 1 的变化。

使用 ++mCurrentPage,您首先将 mCurrentPage 设置为 1,然后才调用 setCurrentItem(1)。下次您单击按钮时,它会将 mCurrentPage 设置为 2 并调用 setCurrentItem(2)

关于java - 按钮仅在第二次触摸后起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47960182/

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