代码之家  ›  专栏  ›  技术社区  ›  Novice

Android onClickListener只工作一次,按钮第二次不工作

  •  0
  • Novice  · 技术社区  · 8 年前

    我是android编程新手,下面是checkButton只工作一次的代码。当我第二次单击它时,什么也没有发生。我似乎找不到问题。

    import android.app.AlertDialog;
    import android.app.ProgressDialog;
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
        ProgressDialog progress;
        Button checkButton;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            checkButton = (Button) findViewById(R.id.button);
            checkButton.setOnClickListener(
                new View.OnClickListener(){
                    @Override
                    public void onClick(View v){
    
                        EditText aNumber = (EditText) findViewById(R.id.editText);
                        String aCount = aNumber.getText().toString();
                        boolean totalDigit = aCount.length()==16;
                        if(totalDigit){
                         if(checkConnectivity()) {
                           progress = new ProgressDialog(v.getContext());
                           progress.setTitle("Validating Number");
                           progress.setMessage("Processing...");
                           progress.setCancelable(true);
                           progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                           progress.show();
                           new Thread(new Runnable() {
                               public void run() {
                                   try {
                                       Thread.sleep(5000);
                                   } catch (Exception e) {
                                       e.printStackTrace();
                                   }
                                   progress.dismiss();
                                   MainActivity.this.runOnUiThread(new Runnable()
                                   {
                                       public void run()
                                       {
                                           //Do your UI operations like dialog opening or Toast here
                                           showAlert();
                                       }
                               });
                           }
                           }).start();
    
                       }else{
                           Toast.makeText(MainActivity.this,"Internet Connection is not Available",Toast.LENGTH_SHORT).show();
                       }}else {
                             Toast.makeText(MainActivity.this, "Please enter correct 16 digit number",Toast.LENGTH_SHORT).show();
                            }
    
                    }
                });
             }
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
             private void showAlert(){
                 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                 
                 builder.setMessage("Number not found!");
                 builder.setCancelable(true);
                
    
                 //Creating dialog box
                 AlertDialog alert = builder.create();
                 //Setting the title manually
                 alert.setTitle("Number Status");
                 alert.show();
                 setContentView(R.layout.activity_main);
    
             }
    
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
           private boolean checkConnectivity(){
               ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
               if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
                       connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
                   return  true;
               }
               else {
                   return false;
               }
           }
    
    
        }

    我的检查按钮只工作一次。在我第二次按下后,什么都没有发生。我在stackoverflow中查看了之前所有与此相关的答案,但没有任何帮助。请有人告诉我出了什么问题。

    2 回复  |  直到 8 年前
        1
  •  5
  •   Truong Giang Dam    8 年前

    我想问题是你打电话 setContentView(R.layout.activity_main); 两次 setContentView 将替代布局并将其替换为新布局。你应该只打一个电话 onCreate . 这就是为什么您应该在showAlert()方法中删除这一行。

    private void showAlert(){
             AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    
             builder.setMessage("Number not found!");
             builder.setCancelable(true);
    
    
             //Creating dialog box
             AlertDialog alert = builder.create();
             //Setting the title manually
             alert.setTitle("Number Status");
             alert.show();
    
             // remove this line below
             //setContentView(R.layout.activity_main);
    
         }
    

    您将收到警告消息 Attempted to finish an input event but the input event receiver has already been disposed 如果您调试代码。原因是你打电话 setContentView 两次,以便前一个视图的输入事件接收器不可用。这就是为什么你不能在第二次点击你的按钮。

    尝试我的解决方案,在showAlert中删除setContentView并查看结果。

        2
  •  0
  •   dazza5000    8 年前

    onClickListener中有两个条件,它们影响onClickListener是否“工作”

    您是否在这些条件之前加入了debug语句,以查看它们是否达到了这个程度(请注意下面的两个if语句):

                    boolean totalDigit = aCount.length()==16;
                    if(totalDigit){
                     if(checkConnectivity()) {