Android,我想在按钮监听里嵌套循环,但是提示i需要是final……
如题,我程序大概是:for(inti=0;i<3;i++){denglu.setOnClickListener(newButton.OnClickListener(){@...
如题,我程序大概是:
for(int i=0;i<3;i++){
denglu.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(yonghuming.getText().toString().equals("wanghb")&&mima.getText().toString().equals("000000")){
//进入***------------------------------------------------------------------------------------
}
else {
if(i<2){
new AlertDialog.Builder(GooglePlayActivity.this)
.setTitle("验证有误")
.setMessage("请重新输入(只有三次机会哦~)")
.setPositiveButton("我知道了",
new DialogInterface.OnClickListener() {
@Override
public void onClick
(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.show();
}
else {
new AlertDialog.Builder(GooglePlayActivity.this)
.setTitle("非常遗憾")
.setMessage("您可能还没有注册,要注册吗?")
.setPositiveButton("不了,谢谢~",
new DialogInterface.OnClickListener() {
@Override
public void onClick
(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
})
.setNegativeButton("我要注册!",
new DialogInterface.OnClickListener() {
@Override
public void onClick
(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
jumpToLayout_zhuce();
}
})
.show();
}
}
}
});
}
我的i是循环变量啊,怎么办? 展开
for(int i=0;i<3;i++){
denglu.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(yonghuming.getText().toString().equals("wanghb")&&mima.getText().toString().equals("000000")){
//进入***------------------------------------------------------------------------------------
}
else {
if(i<2){
new AlertDialog.Builder(GooglePlayActivity.this)
.setTitle("验证有误")
.setMessage("请重新输入(只有三次机会哦~)")
.setPositiveButton("我知道了",
new DialogInterface.OnClickListener() {
@Override
public void onClick
(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.show();
}
else {
new AlertDialog.Builder(GooglePlayActivity.this)
.setTitle("非常遗憾")
.setMessage("您可能还没有注册,要注册吗?")
.setPositiveButton("不了,谢谢~",
new DialogInterface.OnClickListener() {
@Override
public void onClick
(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
})
.setNegativeButton("我要注册!",
new DialogInterface.OnClickListener() {
@Override
public void onClick
(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
jumpToLayout_zhuce();
}
})
.show();
}
}
}
});
}
我的i是循环变量啊,怎么办? 展开
展开全部
您好,一般不用循环来做试着登录了几次,您可以增加:
请看下面代码(我已经做过了测试)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | package com.example.answerforbaidu; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private EditText yonghuming = null ; private EditText mima = null ; private Button loginBtn = null ; //一个计数器,用于同一个用户名试了几次 private int TimesOfClick = 0 ; //保存当前的用户名,用于判断是否是同一个用户名 private String current_yonghuming = "" ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); this .yonghuming = (EditText) super .findViewById(R.id.yonghuming); this .current_yonghuming = yonghuming.getText().toString(); this .mima = (EditText) super .findViewById(R.id.mima); this .loginBtn = (Button) super .findViewById(R.id.loginBtn); this .loginBtn.setOnClickListener( new LoginOnClickListenerImpl()); } private class LoginOnClickListenerImpl implements OnClickListener { @Override public void onClick(View view) { ////另一个用户名,改变当前的用户名,将计数器归位到0 if (!MainActivity. this .yonghuming.getText().toString().equals(MainActivity. this .current_yonghuming)){ MainActivity. this .TimesOfClick = 0 ; MainActivity. this .current_yonghuming = MainActivity. this .yonghuming.getText().toString(); } if (MainActivity. this .yonghuming.getText().toString() .equals( "wanghb" ) && MainActivity. this .mima.getText().toString() .equals( "000000" )) { //进入 } else { //计数器加一 MainActivity. this .TimesOfClick++; if (TimesOfClick <= 3 ){ new AlertDialog.Builder(MainActivity. this ) .setTitle( "验证有误" ) .setMessage( "请重新输入(只有三次机会哦~)" ) .setPositiveButton( "我知道了" , new DialogInterface.OnClickListener() { @Override public void onClick (DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .show(); } else { new AlertDialog.Builder(MainActivity. this ) .setTitle( "非常遗憾" ) .setMessage( "您可能还没有注册,要注册吗?" ) .setPositiveButton( "不了,谢谢!" , new DialogInterface.OnClickListener() { @Override public void onClick (DialogInterface dialog, int which) { // TODO Auto-generated method stub finish(); } }) .setNegativeButton( "我要注册" , new DialogInterface.OnClickListener() { @Override public void onClick (DialogInterface dialog, int which) { // TODO Auto-generated method stub //跳转到注册界面 } }) .show(); } } } } } |
展开全部
1 2 3 4 5 | for ( int i= 0 ;i< 3 ;i++){ final int idx = i; // ..... if (idx < 2 ){ // .... |
展开全部
lz太偷懒了,提示框这块的判断变量就不要用i了,随便在写一个全局变量,然后跟着i变化就行了
1 |
展开全部
你的这段程序思路有问题,你的i与按钮点击逻辑是在不同的线程内,你可以在按钮点击逻辑处理内只做对错的判断,通过Handler将判断结果传回主线程,再做录入错误次数的判断。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询