停止活动(不完成它)

问题描述:

我有下面的代码,简单地说,当按钮被点击一个新的活动打开。我需要做2个检查:停止活动(不完成它)

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 
      } 
     } 
    } 
    } 
} 
+0

你要什么做的,如果在EditText上是不是空的? –

+0

仅显示警报消息(只有一次)。 –

+0

你的for循环的目的是什么? – Jerrol

2 - 如果编辑的文本不是空的,只显示一个警告,不要打开相应的活动(不为我工作)+该for循环会使警报出现3次(字符数)。

如果您不想打开活动并仅运行一次警报,则从循环中取出警报并删除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? 
      } 
     } 

    } 
    } 
} 
+0

谢谢Maihan,但是也许我没有解释清楚自己,我想只有在if(!(c = 0x05d0)通过检查时才会发生startActivity,否则只显示alert,所以我的问题在于startActivity –

+0

@AboelmagdSaadAboelmagd尝试修改后的答案 –

+0

感谢您的提示,我之前尝试过,它完成了这项工作,但警报出现了3次! –

这个固定我的问题

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