dialog

82 篇文章 1 订阅
1 篇文章 0 订阅

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);
    }
}```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学知识拯救世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值