簡體   English   中英

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

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

我是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中已經找到了所有與此相關的答案,但是沒有任何幫助。 請有人告訴我出了什么問題。

我認為問題是您致電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如果您調試代碼, Attempted to finish an input event but the input event receiver has already been disposed 原因是您兩次調用setContentView ,因此上一個視圖的輸入事件接收器不是空閑的。 這就是為什么您不能第二次單擊按鈕。

試試我的解決方案,在showAlert中刪除setContentView並查看結果。

您的onClickListener中有兩個條件,它們會影響onClicklistener是否“起作用”

您是否在這些條件語句之前放入了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