2017-10-28 122 views
1

我有下面的代碼,簡單地說,當按鈕被點擊一個新的活動打開。我需要做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

回答

0

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 <= 0x05ea && c> = 0x05d0)通過檢查時纔會發生startActivity,否則只顯示alert,所以我的問題在於startActivity –

+0

@AboelmagdSaadAboelmagd嘗試修改後的答案 –

+0

感謝您的提示,我之前嘗試過,它完成了這項工作,但警報出現了3次! –

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