我是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中查看过所有与此相关的答案,但没有帮助我。请有人告诉我出了什么问题。
答案 0 :(得分:5)
我认为问题是你两次致电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并查看结果。
答案 1 :(得分:0)
onClickListener中有两个条件会影响onClicklistener"是否正常工作"
您是否在这些条件之前放入调试语句以查看它们是否达到此目的(请注意下面的两个if语句):
boolean totalDigit = aCount.length()==16;
if(totalDigit){
if(checkConnectivity()) {