将EditText中的文本与定义的String进行比较?

时间:2012-10-25 16:43:21

标签: android

我是Android的新手,我正在编写一个简单的闹钟应用程序。当我在EditText中输入字符串时,如“10:00,Monday”这个字符串将比较语法与定义的字符串,如果是真的话'打开AlarmClock。但我不知道如何比较。你能给我一个想法吗?非常感谢。我的应用程序的界面和代码在下面

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

公共类MainActivity扩展了Activity {

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

2 个答案:

答案 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
}

这将返回一个布尔值。

答案 1 :(得分:3)

if(myEditText.getText()。toString()。equals(myString)){...}