1

所以我用 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 扩展 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;
}

}

4

1 回答 1

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/

于 2012-07-30T21:06:55.737 回答