停止活动(不完成)

时间:2017-10-28 22:56:00

标签: android for-loop

我有下面的代码,简单地说,当点击按钮时,会打开一个新活动。我需要做两次检查:

1-如果编辑文本为空,只需显示提醒并且不要打开活动(这对我来说很完美)

2-如果编辑文本不为空,只显示一个警告并且不打开活动(对我不起作用)+ for循环使警报出现3次(字符数)

我尝试使用finish(),但它没有帮助。 与我在Java中使用的相同代码(e.consume)并且完美地运行。

这是我的代码:

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.ActionMode;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import static android.provider.AlarmClock.EXTRA_MESSAGE;

public class Paal extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_paal);
}

// Action the button PAST will do
// Show the result of paal in the past
public void openPaalPast(View paalThePast) {
    // Shows the past conjungtion
    Intent paalPastView;
    paalPastView = new Intent(this, PastOfPaal.class);
    EditText getTheVerb = (findViewById(R.id.editText1));
    String theVerb = getTheVerb.getText().toString();
    // If the text is empty
    if (theVerb.isEmpty()) {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setMessage("alert");
        alert.setTitle("No");
        alert.setPositiveButton("OK", null);
        alert.setCancelable(true);
        alert.create().show();
    }
    // If the text is not empty
    if (!theVerb.isEmpty()) {
        startActivity(paalPastView);
        char[] charArray = theVerb.toCharArray();
        for (char c : charArray) {
            if (!(c <= 0x05ea && c >= 0x05d0)) {
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setMessage("alert");
                alert.setTitle("Noo");
                alert.setPositiveButton("OK", null);
                alert.setCancelable(true);
                alert.create().show();
                break; // to stop the for loop (so that the alert will be shown only once.
                // finish(); //doesn't help much, it only kill the whole activity
            }
        }
    }
  }
}

2 个答案:

答案 0 :(得分:0)

  

2-如果编辑文本不为空,只需显示提醒并且不打开活动(对我不起作用)+ for循环使警报出现3次(数字) (Chars)。

如果您不想打开活动并仅运行一次警报,请从循环中取出警报并删除startActivity。

public void openPaalPast(View paalThePast) {
    // Shows the past conjungtion
    Intent paalPastView;
    paalPastView = new Intent(this, PastOfPaal.class);
    EditText getTheVerb = (findViewById(R.id.editText1));
    String theVerb = getTheVerb.getText().toString();
    // If the text is empty
    if (theVerb.isEmpty()) {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setMessage("alert");
        alert.setTitle("No verb entered");
        alert.setPositiveButton("OK", null);
        alert.setCancelable(true);
        alert.create().show();
    } else {
    // If the text is not empty
        char[] charArray = theVerb.toCharArray();
        for (char c : charArray) {
            if (!c <= 0x05ea && c >= 0x05d0) {
                 startActivity(paalPastView);  // Here I want the activity not to start, so Is there a way to do so?
            }
        }

    }
  }
}

答案 1 :(得分:0)

这解决了我的问题

        // If the text is not empty
        char[] charArray = theVerb.toCharArray();
        for (char c : charArray) {
            if ((!theVerb.isEmpty()) && (c <= 0x05ea && c >= 0x05d0)) {
                startActivity(paalPastView);
                break;
            } else if (!(c <= 0x05ea && c >= 0x05d0)){
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setMessage("alert");
                alert.setTitle("No");
                alert.setPositiveButton("OK", null);
                alert.setCancelable(true);
                alert.create().show();
                break;
            }
        }