gpt4 book ai didi

java - 时间/日期对话框。尝试添加按钮。 Eclipse 安卓

转载 作者:行者123 更新时间:2023-12-01 05:13:14 25 4
gpt4 key购买 nike

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

我用它来让日期/时间对话框正常工作:

public class RainbowBookActivity extends 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);

}
});
}

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

*预订 Activity :

public class BookingsActivity extends 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;
}

}

最佳答案

 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/

关于java - 时间/日期对话框。尝试添加按钮。 Eclipse 安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11729150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com