时间/日期对话框。试图添加按钮。 Eclipse Android

时间:2012-07-30 20:34:05

标签: java android eclipse

所以我用textViews,editTexts,日期对话框和时间对话框选择器创建了一个Activity。加一个按钮。

我使用它来使日期/时间对话框起作用:

  

公共类RainbowBookActivity扩展了Activity {

private static final int DATE_DIALOG_ID=1; // for date 
  private static final  int TIME_DIALOG_ID=2; // for month 
  private int d,mo,y,h,m; // for date & month variables 
  Button b1,b2; // button objects 
  TextView e1,e2;  // textview objects

  // execution starts from here 
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rainbowbook);  // calling activity_rainbowbook.xml 
     e1=(TextView)findViewById(R.id.textview1); // getting textview1 id from activity_rainbowbook.xml 
     b1=(Button)findViewById(R.id.button1); // getting  button id from activity_rainbowbook.xml

   b1.setOnClickListener(new OnClickListener() // setting listener for button one  

   {

   public void onClick(View arg0) {


    // TODO Auto-generated method stub
    showDialog(DATE_DIALOG_ID);   // generating dialog box 
     }
  });

  final Calendar cal=Calendar.getInstance(); // allocating memory for calendar instance
   d=cal.get(Calendar.DAY_OF_MONTH); // present date        
  mo=cal.get(Calendar.MONTH); // present month
     y=cal.get(Calendar.YEAR); // present year
     updateDate();  // update date 

  b2=(Button)findViewById(R.id.button2); // getting listener for button2 
   e2=(TextView)findViewById(R.id.textview2); 
   b2.setOnClickListener(new OnClickListener() // setting listener for button2 

   {
    public void onClick(View arg0) {


    // TODO Auto-generated method stub
    showDialog(TIME_DIALOG_ID);
 }
});

  h=Calendar.getInstance().get(Calendar.HOUR); // getting  present hour & minute
  m=Calendar.getInstance().get(Calendar.MINUTE);
  updateTime();  // updating time 
}


public void updateTime()
{

   e2.setText(new StringBuilder().append(h).append(':').append(m));

}
public void updateDate()
{

   e1.setText(new StringBuilder().append(d).append('/').append(mo+1).append('/').append(y));

  }


private DatePickerDialog.OnDateSetListener datelistener=new DatePickerDialog.OnDateSetListener() 
{


   public void onDateSet(DatePicker view,int year, int monthofyear, int day) 
   {

                     y=year;
                     mo=monthofyear;
                     d=day;
                     updateDate();
  }
 };

  private TimePickerDialog.OnTimeSetListener timelistener=new TimePickerDialog.OnTimeSetListener() 
  {
   public void onTimeSet(TimePicker view, int hourofday, int minute) 
   {
    // TODO Auto-generated method stub
    h=hourofday;
    m=minute;
     updateTime();
   }
 };

 protected Dialog onCreateDialog(int id) 
 {
    switch(id)
    {
      case DATE_DIALOG_ID:
                   return new DatePickerDialog(this,datelistener , y, mo, d);


      case TIME_DIALOG_ID:
                  return new TimePickerDialog(this,timelistener,h,m,false);
  }
  return null;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_rainbowbook, menu);
    return true;
}

}

现在我想知道如何在这里得到这个,所以我可以有一个从editTexts传输信息的按钮:

    EditText inputName;
    EditText inputRoom;

    public void onCreate1(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rainbowbook);

        inputName = (EditText) findViewById(R.id.editText1);
        inputRoom = (EditText) findViewById(R.id.editText2);
        Button btnNextScreen = (Button) findViewById(R.id.button3);

        //Listening to button event
        btnNextScreen.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                //Starting a new Intent
                Intent nextScreen = new Intent(getApplicationContext(), BookingsActivity.class);

                //Sending data to another Activity
                nextScreen.putExtra("name", inputName.getText().toString());
                nextScreen.putExtra("room", inputRoom.getText().toString());

                Log.e("n", inputName.getText()+"."+ inputRoom.getText());

                startActivity(nextScreen);

            }
        });
    }

我似乎无法弄清楚这一点,并希望有人可以帮助我。感谢。

* BookingsActivity:

  

公共类BookingsActivity扩展了Activity {

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bookings);

        TextView Name = (TextView) findViewById(R.id.textView2);
        TextView Room = (TextView) findViewById(R.id.textView3);
        Button btnClose = (Button) findViewById(R.id.btnClose);

        Intent i = getIntent();
        // Receiving the Data
        String name = i.getStringExtra("name");
        String room = i.getStringExtra("room");
        Log.e("Bookings", name + "." + room);

        // Displaying Received data
        Name.setText(name);
        Room.setText(room);

        // Binding Click event to Button
        btnClose.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                //Closing SecondScreen Activity
                finish();
            }
        });

    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_bookings, menu);
    return true;
}

}

1 个答案:

答案 0 :(得分:0)

 maybe this in your BookingActivity

 Bundle b = i.getExtras();
 String name = b.getString("name", "default name");
 String room = b.getString("room", "default room");

编辑: 另外,使用startActivityForResult()激活BookingActivity,这样你就可以在完成后再从中获取数据。

编辑: 可以找到startActivityForResult的示例How to return a result (startActivityForResult) from a TabHost Activity?http://android.rahulblogs.com/android-startactivityforresult-example/