如何在同一个活动中使用 ViewFlipper(或)ViewSwitcher 和 TextSwitcher..?
我是 Android 开发的初学者。我能够实现 textSwitcher 和 ViewSwitcher ,但单独在一个活动中。我希望它们在同一个活动中发挥作用。
此代码不会在 IDE 中给出错误,但会在模拟器中给出“强制关闭”错误。
导入后的代码和所有..
ViewFlipper vS1,vS2,vS3;
Button onep,twop,lose,win,help,settings,start,pick1,pick2;
int condn,total,max=5,min=1,rem;
TextSwitcher minte,maxte,select1,select2,coinsrem;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Animations
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right);
// Text Views ----
minte.findViewById(R.id.min);
maxte.findViewById(R.id.max);
minte.setFactory(new TextSwitcherFactory());
maxte.setFactory(new TextSwitcherFactory());
minte.setText("Hello");
// switching Views ----
vS1 =(ViewFlipper) findViewById(R.id.vs1);
vS2 =(ViewFlipper) findViewById(R.id.vs2);
vS3 =(ViewFlipper) findViewById(R.id.vs3);
vS1.setInAnimation(in);
vS1.setOutAnimation(out);
vS2.setInAnimation(in);
vS2.setOutAnimation(out);
vS3.setInAnimation(in);
vS3.setOutAnimation(out);
// create RangeSeekBar as Integer range between 20 and 75
RangeSeekBar<Integer> seekBar = new RangeSeekBar<Integer>(20, 75, getBaseContext());
seekBar.setOnRangeSeekBarChangeListener(this);
// add RangeSeekBar to pre-defined layout
ViewGroup layout = (ViewGroup) findViewById(R.id.rangeseek);
layout.addView(seekBar);
//----buttons-------------------------
win = (Button)findViewById(R.id.win);
lose = (Button)findViewById(R.id.loses);
win.setOnClickListener(this);
lose.setOnClickListener(this);
//----buttons end----------------------
//---Animating the views---
}
@Override
public void rangeSeekBarValuesChanged(Integer minValue, Integer maxValue) {
// TODO Auto-generated method stub
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.win:
condn = 1;
vS2.showNext();
break;
case R.id.loses :
condn =0;
vS2.showNext();
break;
}
}
public class TextSwitcherFactory implements ViewSwitcher.ViewFactory{
@Override
public View makeView() {
// TODO Auto-generated method stub
TextView t = new TextView(LastActivity.this);
t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
t.setTextSize(36);
t.setTextColor(Color.argb(255, 255, 204, 51));
return t; }
}
Im a begineer in android dev. Im able to implement textSwitcher and ViewSwitcher ,but individually in an activity. I want them to function in the same activity.
This code doesn't give me errors in the IDE but its "Force Closing" in the emulator.
The code after the imports and all..
ViewFlipper vS1,vS2,vS3;
Button onep,twop,lose,win,help,settings,start,pick1,pick2;
int condn,total,max=5,min=1,rem;
TextSwitcher minte,maxte,select1,select2,coinsrem;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Animations
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right);
// Text Views ----
minte.findViewById(R.id.min);
maxte.findViewById(R.id.max);
minte.setFactory(new TextSwitcherFactory());
maxte.setFactory(new TextSwitcherFactory());
minte.setText("Hello");
// switching Views ----
vS1 =(ViewFlipper) findViewById(R.id.vs1);
vS2 =(ViewFlipper) findViewById(R.id.vs2);
vS3 =(ViewFlipper) findViewById(R.id.vs3);
vS1.setInAnimation(in);
vS1.setOutAnimation(out);
vS2.setInAnimation(in);
vS2.setOutAnimation(out);
vS3.setInAnimation(in);
vS3.setOutAnimation(out);
// create RangeSeekBar as Integer range between 20 and 75
RangeSeekBar<Integer> seekBar = new RangeSeekBar<Integer>(20, 75, getBaseContext());
seekBar.setOnRangeSeekBarChangeListener(this);
// add RangeSeekBar to pre-defined layout
ViewGroup layout = (ViewGroup) findViewById(R.id.rangeseek);
layout.addView(seekBar);
//----buttons-------------------------
win = (Button)findViewById(R.id.win);
lose = (Button)findViewById(R.id.loses);
win.setOnClickListener(this);
lose.setOnClickListener(this);
//----buttons end----------------------
//---Animating the views---
}
@Override
public void rangeSeekBarValuesChanged(Integer minValue, Integer maxValue) {
// TODO Auto-generated method stub
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.win:
condn = 1;
vS2.showNext();
break;
case R.id.loses :
condn =0;
vS2.showNext();
break;
}
}
public class TextSwitcherFactory implements ViewSwitcher.ViewFactory{
@Override
public View makeView() {
// TODO Auto-generated method stub
TextView t = new TextView(LastActivity.this);
t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
t.setTextSize(36);
t.setTextColor(Color.argb(255, 255, 204, 51));
return t; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有意义...
你应该像这样初始化它们:
doesn't make sense...
You should initialaze them like:
从您的代码来看,您似乎正在尝试初始化 3 个单独的 ViewFlippers 并在它们之间切换。这不是如何做到这一点。 ViewFlipper(或 ViewSwitcher)是 FrameLayout 类的扩展。因此,它一次只会显示 1 个子视图。
要修复您的代码,请仅定义一个 ViewFlipper。在其下,定义您想要在之间切换的 3 个视图(即,RelativeLayout、LinearLayout 等)。
另外,看起来您也在尝试制作一些动画。因此,您最终可能想使用 ViewAnimator 类,它只是 ViewFlipper 的父类。
谷歌一些教程。那里有一些好的。
我希望这有帮助!
From your code, it looks like you're attempting to initialize 3 separate ViewFlippers and switch between them. This is not how to do this. A ViewFlipper (or ViewSwitcher) is an extension of the FrameLayout class. So, it will only show 1 child view at a time.
To fix your code, define only a single ViewFlipper. Under that, define the 3 views (i.e. RelativeLayout, LinearLayout, etc) that you want to switch between.
Also, it looks like you're trying to do some animation as well. So, you may ultimately want to use the ViewAnimator class instead, which is just the parent class of ViewFlipper.
Google for some tutorials. There are some good ones out there.
I hope this helped!