简体   繁体   English

Android onClickListener只能运行一次,Button不能再次运行

[英]Android onClickListener works only once, Button does not works for second time

I am new in android Programming , Here is the code in which checkButton works only for once . 我是android编程的新手,这是checkButton仅可使用一次的代码。 Nothing happens when I click it for the second time. 当我第二次单击时没有任何反应。 I can't seem to find problem. 我似乎找不到问题。

 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; } } } 

My Check Button works for only once. 我的检查按钮只能使用一次。 After I press it for second time nothings happen. 在我第二次按下它之后,什么也没有发生。 I have looked all previous answer related to this in stackoverflow but nothings helped me. 我在stackoverflow中已经找到了所有与此相关的答案,但是没有任何帮助。 Please someone tell me what is going wrong. 请有人告诉我出了什么问题。

I think the problem is you call setContentView(R.layout.activity_main); 我认为问题是您致电setContentView(R.layout.activity_main); twice. 两次。 setContentView will override the layout and replace it with the new one. setContentView将覆盖该布局,并将其替换为新的布局。 You should only one call it in onCreate . 您只能在onCreate调用它。 That's why you should remove this line in showAlert() method. 这就是为什么您应该在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);

     }

You will get the message warning Attempted to finish an input event but the input event receiver has already been disposed if you debug your code. 您将收到警告消息“ Attempted to finish an input event but the input event receiver has already been disposed如果您调试代码, Attempted to finish an input event but the input event receiver has already been disposed The reason is you call setContentView twice so that the input event receiver of previous view is not free. 原因是您两次调用setContentView ,因此上一个视图的输入事件接收器不是空闲的。 That's why you can not click your button in the second times. 这就是为什么您不能第二次单击按钮。

Try my solution, remove setContentView in showAlert and see the result. 试试我的解决方案,在showAlert中删除setContentView并查看结果。

You have two conditionals in your onClickListener which affect whether the onClicklistener "works" 您的onClickListener中有两个条件,它们会影响onClicklistener是否“起作用”

Have you put in debug statements before these conditionals to see if they get this far (notice the two if statements below): 您是否在这些条件语句之前放入了debug语句,以查看它们是否达到此程度(请注意下面的两个if语句):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM