EditText(textfield)在报警应用中不起作用?

时间:2014-04-17 08:45:33

标签: java android android-edittext alarm

以下是我的代码:

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class AlarmMainActivity extends Activity {
    Calendar cal = Calendar.getInstance();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
       setContentView(R.layout.main);

   Button set = (Button)findViewById(R.id.button1);
   EditText Time = (EditText)findViewById(R.id.time);
   int n =Integer.parseInt(Time.getText().toString());

    //Create an offset from the current time in which the alarm will go off.   
    cal.add(Calendar.SECOND, 10);

    //Create a new PendingIntent and add it to the AlarmManager
    Intent intent = new Intent(this, AlarmReceiverActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,
        n, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = 
        (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            pendingIntent);     
}

}

此代码中发生的情况是此应用程序在当前时间自动设置10秒的警报。我希望用户在需要多少秒后输入。我尝试用cal.add(Calendar.SECOND, 10);代替10.但是它没有用。

2 个答案:

答案 0 :(得分:2)

您必须将此int n =Integer.parseInt(Time.getText().toString());置于某个Button click事件

如下所示

ib_load.setOnClickListener(new  View.OnClickListener() {

    @Override
    public void onClick(View v) {
    int n =Integer.parseInt(Time.getText().toString());
     cal.add(Calendar.SECOND, n);

    }
});

答案 1 :(得分:1)

你可以做一件事,我已经创建了另一个类

Home.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Home extends Activity{

    static int n = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);


        Button go = (Button)findViewById(R.id.button1);
        go.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                EditText sec = (EditText)findViewById(R.id.editText1);
                n = Integer.parseInt(sec.getText().toString());
                Intent go = new Intent("com.nerdwin15.demo.alarmdemo.AlarmMainActivity");
                startActivity(go);
            }
        });
    }

}

AlarmMainActivity.java

public class AlarmMainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
       setContentView(R.layout.main);

        //Create an offset from the current time in which the alarm will go off.
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.SECOND, Home.n);

        //Create a new PendingIntent and add it to the AlarmManager
        Intent intent = new Intent(this, AlarmReceiverActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
            12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = 
            (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                pendingIntent);
    }

}

现在你输入家庭课的时间,然后去另一个。确保在清单中更改你的LAUNCHER它应该是Home.java