实际上,我正在尝试实施This。我遇到了一些问题,当我点击Image Circle 1
按钮时,Passcode
字段会更改第1个字段的背景颜色。但是,当我点击图像圈1 Button
时,第二个Passcode
字段没有变化。我认为getId()
正常无法正常工作。可能我知道实现目标的正确方法是什么?这个问题太基础了,但我找不到合适的解决方案。请帮帮我。请查看此声明View.OnClickListener imgButtonHandlerone = new View.OnClickListener()
这是我的代码:
public class UserPasscode extends Activity {
ImageButton Imagepassone ;
ImageButton Imagepasstwo ;
ImageButton Imagepassthree ;
ImageView image;
ImageView image2;
ImageView image3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_passcode);
ActionBar ab = getActionBar();
ab.setDisplayUseLogoEnabled(false);
ab.setDisplayShowHomeEnabled(true);
ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ab.setCustomView(R.layout.actionbar);
ab.setDisplayHomeAsUpEnabled(true);
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#33CCFF"));
ab.setBackgroundDrawable(colorDrawable);
Imagepassone = (ImageButton) findViewById(R.id.ButtonPassOne);
Imagepasstwo = (ImageButton) findViewById(R.id.ButtonPassTwo);
Imagepassthree = (ImageButton) findViewById(R.id.ButtonPassThree);
Imagepassone.setOnClickListener(imgButtonHandlerone);
Imagepasstwo.setOnClickListener(imgButtonHandlertwo);
Imagepassthree.setOnClickListener(imgButtonHandlerthree);
image = (ImageView) findViewById(R.id.imagefirstpasscode);
image2 = (ImageView) findViewById(R.id.imagesecondpasscode);
image3=(ImageView)findViewById(R.id.imagethirdpasscode);
}
View.OnClickListener imgButtonHandlerone = new View.OnClickListener() {
public void onClick(View v) {
if(v.isPressed()){
int d ;
d = R.drawable.passselect;
if(image.getId()!=d)
{
image.setImageResource(d);
}
if(image.getId()==d && (image2.getId()!=d))
{
image2.setImageResource(d);
}
}
}
};
View.OnClickListener imgButtonHandlertwo = new View.OnClickListener() {
public void onClick(View v) {
}
};
View.OnClickListener imgButtonHandlerthree = new View.OnClickListener() {
public void onClick(View v) {
}
};
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, UserDobActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
moveTaskToBack(true);
UserPasscode.this.finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.user_passcode, menu);
return true;
}}
答案 0 :(得分:1)
d
不是id
。我建议你这样做:
// same listener for everyone
Imagepassone.setOnClickListener(imgButtonHandler);
Imagepasstwo.setOnClickListener(imgButtonHandler);
Imagepassthree.setOnClickListener(imgButtonHandler);
//...
// switch id for each view inside the same listener
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
// check the view clicked by id
if(v.getId() == R.id.Imagepassone) {
// do something like change background
if(image.getBackground() != getResources().getDrawable(R.drawable.passselect)) {
image.setBackgroundResource(R.drawable.passselect);
} else if((image.getBackground() == getResources().getDrawable(R.drawable.passselect)) &&
(image2.getBackground() != getResources().getDrawable(R.drawable.passselect)) {
image.setBackgroundResource(R.drawable.passselect);
}
} else if(v.getId() == R.id.Imagepasstwo) {
// do something
} else if(v.getId() == R.id.Imagepassthree) {
// do something
}
}
};
但我认为这不是一个正确的方法..
我可以看到并理解,你的图像(图像,图像2,图像3)是顶部的圆形图像,对吧?如果是,您可以尝试通过每次点击更改上面这些图标的步骤并更改其背景。我认为用本地int
执行此操作会更好,并更改步骤。这样的事情应该有效:
// local variable just below ImageView image3; line
private int nbStep = 0;
// ...
Imagepassone.setOnClickListener(imgButtonHandler);
Imagepasstwo.setOnClickListener(imgButtonHandler);
Imagepassthree.setOnClickListener(imgButtonHandler);
// listener
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
// check id like above
if(v.getId() == R.id.Imagepassone) {
// augment the local int
nbStep++;
// call a general method to check the background with int
changeStepBackground(nbStep);
}
}
});
public void changeStepBackground(int i) {
// four step switch
switch(i) {
case 1:
image.setBackgroundResource(R.drawable.passselect);
break;
case 2:
image2.setBackgroundResource(R.drawable.passselect);
break;
case 3:
image3.setBackgroundResource(R.drawable.passselect);
break;
case 4:
image4.setBackgroundResource(R.drawable.passselect);
break;
}
}
我希望我明白这一点,这会有所帮助。