2012-10-25 42 views
0

我是新來的Android和我正在寫一個簡單的鬧鐘應用程序。當我輸入字符串像EditText這樣的「10:00,星期一」這個字符串將比較語法與定義的字符串,如果它真的會打開AlarmClock。但我不知道如何比較。你可以給我一個主意嗎?非常感謝。我的應用程序的界面和代碼下​​面將EditText中的文本與已定義的字符串進行比較?

1.My應用程序的界面https://dl.dropbox.com/u/40382482/Screenshot%20from%202012-10-25%2023%3A51%3A14.png

公共類MainActivity擴展活動{

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    /* Calendar cal = Calendar.getInstance(); 
    SimpleDateFormat date = new SimpleDateFormat("dd/MM/yyyy"); 
    SimpleDateFormat time = new SimpleDateFormat("hh:mm:ss"); 

    final TextView labelDate = (TextView) findViewById(R.id.lblDate); 
    final TextView lableTime = (TextView) findViewById(R.id.lblTime); 

    labelDate.setText(date.format(cal.getTime())); 
    lableTime.setText(time.format(cal.getTime()));*/ 
    final ArrayList<String> setAlarm = new ArrayList<String>(); 
    //nhap noi dung vao edit text 

    final EditText alarmEnter = (EditText) findViewById(R.id.setting); 

    final Button button = (Button) findViewById(R.id.Okay); 

    OnClickListener add = new OnClickListener() 
    { 

     public void onClick(View v) 
     { 
      if(alarmEnter.getText().toString().equals("")) 
      { 
       AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
       builder.setTitle("Info Missing"); 
       builder.setMessage("Please Enter All Information"); 
       builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() 
       { 
        public void onClick(DialogInterface diablog, int which) 
        { 

        } 
       } 
         ); builder.show(); 
      } 
      else { 
           //compare string in EditText and defined string? 

      } 
     } 
    }; 
    button.setOnClickListener(add); 
} 
+1

請分享你迄今爲止編寫的代碼。 – 13hsoj

+1

'if(myDefinedString.equals(myEditText.getText()。toString()){// do something}' – Squonk

+0

哦,謝謝!我正在嘗試 –

回答

5

要比較一個字符串與另一個,使用:

if(strText.equals("myString")){ // strText is the string from the edit text, myString is the string 
           // you are comparing it to 
    // do something 

}else{ 

// do something else 
} 

這將返回一個布爾值。

+0

感謝您的幫助! –

+0

沒問題。 –

1

如果(myEditText.getText()。的toString()。等於(MyString的)){...}

相關問題