Dialog
AlertDialog
构造器模式的AlertDialog
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView textView2;
private TextView textView;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
button.setOnClickListener(this);
}
private void initView() {
textView2 = (TextView) findViewById(R.id.textView2);
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
}
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("see you");
builder.setMessage("真的要再见吗?");
builder.setPositiveButton("相见是缘", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
textView.setText("莫愁天涯无知己!!!!");
}
});
builder.setNegativeButton("nonono", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
textView.setText("听话!!!");
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
DatePicker
public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "JJWorld.MainActivity2";
private DatePicker datePicker;
private Button button2;
private TextView textView3;
private Button button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
button2.setOnClickListener(this);
button3.setOnClickListener(this);
String a = "123456";
String b = "124";
Log.e(TAG,"index: " + a.indexOf(b));
}
private void initView() {
datePicker = (DatePicker) findViewById(R.id.datePicker);
button2 = (Button) findViewById(R.id.button2);
textView3 = (TextView) findViewById(R.id.textView3);
button3 = (Button) findViewById(R.id.button3);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button2:
String desc = String.format("您选择的日期为%s年%s月%s日", datePicker.getYear(),
datePicker.getMonth() + 1, datePicker.getDayOfMonth());
textView3.setText(desc);
break;
case R.id.button3:
break;
}
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<DatePicker
android:id="@+id/datePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:datePickerMode="calendar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:layout_editor_absoluteX="0dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="日期选择器"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.85"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/datePicker"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="日期对话框"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.201"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/datePicker"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
DatePickerDialog
public class MainActivity2 extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener {
private static final String TAG = "JJWorld.MainActivity2";
private DatePicker datePicker;
private Button button2;
private TextView textView3;
private Button button3;
private Button TestThread;
private static boolean flag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
button2.setOnClickListener(this);
button3.setOnClickListener(this);
TestThread.setOnClickListener(this);
}
private void initView() {
datePicker = (DatePicker) findViewById(R.id.datePicker);
button2 = (Button) findViewById(R.id.button2);
textView3 = (TextView) findViewById(R.id.textView3);
button3 = (Button) findViewById(R.id.button3);
TestThread = (Button) findViewById(R.id.TestThread);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button2:
String desc = String.format("您选择的日期为%s年%s月%s日", datePicker.getYear(),
datePicker.getMonth() + 1, datePicker.getDayOfMonth());
textView3.setText(desc);
break;
case R.id.button3:
Calendar instance = Calendar.getInstance();
int year = instance.get(Calendar.YEAR);
int month = instance.get(Calendar.MONTH);
int day = instance.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this, this, year, month, day);
datePickerDialog.show();
break;
case R.id.TestThread:
break;
}
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
String desc = String.format("您选择的日期为%s年%s月%s日", year,
month + 1, dayOfMonth);
textView3.setText(desc);
}
}```