从timePicker获取时间并使用当前时间检查它的问题

时间:2016-11-15 16:18:31

标签: java android

我做了一个小型的android应用程序,它通过timePicker从用户那里抽出时间,如果所选时间与当前时间相同,则应用程序会向您发送通知。这是我在android studio上编写的代码:

public Button mb;
public EditText editText;
public Button pb;
int i=0;

public void showMotivation(){

    mb = (Button)findViewById(R.id.mb);
    editText = (EditText)findViewById(R.id.editText);

    mb.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    editText.setText(MotivationWords());
                }
            }
    );

}

public String MotivationWords(){

    Random rnd = new Random();
    List l = new LinkedList<>();
    l.add("The price of excellence is discipline; the cost of mediocrity is disappointment.");
    l.add("If we had no winter, the spring would not be so pleasant; if we did not sometimes taste of adversity, prosperity would not be so welcome.");
    l.add("If your ship doesn’t come in, swim out to it!");
    l.add("Tomorrow will be a better day ..");
    l.add("Life’s not about waiting for the storms to pass... it’s about learning to dance in the rain.");
    l.add("When you reach the end of your rope, tie a knot in it and hang on.");
    l.add("A smooth sea never made a skilled mariner.");
    l.add("Great masters merit emulation, not worship.");
    l.add("Confidence is contagious. So is the lack of confidence.");
    l.add("Optimism may sometimes be delusional, but pessimism is always delusional.");

    i=rnd.nextInt()%9;

    return l.get(i).toString();


}

public void changing(){

    pb = (Button)findViewById(R.id.pb);
    pb.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intb = new Intent(MainActivity.this, Main2Activity.class);
                    startActivity(intb);
                }
            }
    );

}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    changing();
    showMotivation();
}

1 个答案:

答案 0 :(得分:0)

public Button tb;
public Button bb;
public TimePicker timep;
public int h,m;
public int curh, curm;
public Calendar c;
NotificationCompat.Builder mbuilder = new NotificationCompat.Builder(this);
public int i;



public void change2(){

    bb = (Button)findViewById(R.id.bb);
    bb.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intbb = new Intent(Main2Activity.this, MainActivity.class);
                    startActivity(intbb);
                }
            }
    );

}

public String MotivationWords(){

    Random rnd = new Random();
    List l = new LinkedList<>();
    l.add("The price of excellence is discipline; the cost of mediocrity is disappointment.");
    l.add("If we had no winter, the spring would not be so pleasant; if we did not sometimes taste of adversity, prosperity would not be so welcome.");
    l.add("If your ship doesn’t come in, swim out to it!");
    l.add("Tomorrow will be a better day ..");
    l.add("Life’s not about waiting for the storms to pass... it’s about learning to dance in the rain.");
    l.add("When you reach the end of your rope, tie a knot in it and hang on.");
    l.add("A smooth sea never made a skilled mariner.");
    l.add("Great masters merit emulation, not worship.");
    l.add("Confidence is contagious. So is the lack of confidence.");
    l.add("Optimism may sometimes be delusional, but pessimism is always delusional.");

    i=rnd.nextInt()%9;

    return l.get(i).toString();


}

public void takeTime(){
    timep = (TimePicker)findViewById(R.id.timep);

    timep.setOnTimeChangedListener(
            new TimePicker.OnTimeChangedListener() {
                @Override
                public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

                    h = hourOfDay;
                    m = minute;
                    curh = c.get(Calendar.HOUR);
                    curm = c.get(Calendar.MINUTE);

                    if( (h==curh) && (m == curm) ){
                        mbuilder.setContentTitle("Your Motivation For now :");
                        mbuilder.setContentText(MotivationWords());
                    }
                }
            }
    );

}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    change2();
    tb = (Button)findViewById(R.id.tb);
    tb.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    takeTime();
                }
            }
    );

}