我有下面的代碼,簡單地說,當按鈕被點擊一個新的活動打開。我需要做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
}
}
}
}
}
你要什麼做的,如果在EditText上是不是空的? –
僅顯示警報消息(只有一次)。 –
你的for循環的目的是什麼? – Jerrol