您的位置:

Android弹窗详解

一、Android弹窗时间

Android弹窗可以根据自己的需要来设置显示的时间,主要有以下几种方式:

1. 使用CountDownTimer倒计时器


CountDownTimer countDownTimer = new CountDownTimer(5000, 1000) {
    @Override
    public void onTick(long millisUntilFinished) {
        // 每隔一秒回调一次
    }

    @Override
    public void onFinish() {
        // 倒计时结束时回调
    }
};
countDownTimer.start();

2. 使用Handler.postDelayed()方法


Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // 等待指定时间后执行的操作
    }
}, 5000);

3. 使用Timer.schedule()方法


TimerTask task = new TimerTask() {
    @Override
    public void run() {
        // 需要执行的操作
    }
};
Timer timer = new Timer();
timer.schedule(task, 5000);

二、Android弹窗ListView

在Android弹窗中使用ListView可以展示大量数据,同时也可以进行选择。

1. 创建ListView布局文件


<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</ListView>

2. 在代码中设置ListView的Adapter


ListView listView = findViewById(R.id.listview);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new String[]{"item1", "item2", "item3"});
listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // 点击ListView中的某一项时执行的操作
    }
});

三、Android弹窗代码

Android中弹窗可以使用AlertDialog或PopupWindow来实现,主要有以下几种方式:

1. 使用AlertDialog.Builder创建AlertDialog


AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title")
        .setMessage("Message")
        .setIcon(R.mipmap.ic_launcher)
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 点击确定按钮时执行的操作
            }
        })
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 点击取消按钮时执行的操作
            }
        });
AlertDialog alertDialog = builder.create();
alertDialog.show();

2. 使用PopupWindow创建弹窗


PopupWindow popupWindow = new PopupWindow(this);
popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setContentView(view);
popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);

四、Android弹窗选择控件

在弹窗中添加选择控件可以让用户方便地选择想要的内容。

1. 使用Spinner创建下拉框


Spinner spinner = findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, new String[]{"item1", "item2", "item3"});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // 选择下拉框中的某一项时执行的操作
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
});

2. 使用RadioGroup创建单选框


<RadioGroup
    android:id="@+id/radio_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/radio_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="item1" />

    <RadioButton
        android:id="@+id/radio_button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="item2" />

    <RadioButton
        android:id="@+id/radio_button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="item3" />
</RadioGroup>

RadioGroup radioGroup = findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // 选择单选框中的某一项时执行的操作
    }
});

五、Android弹窗动画

在弹窗中加入动画可以增加用户体验。

1. 使用AnimationSet组合动画


AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, 100);
translateAnimation.setDuration(500);
animationSet.addAnimation(translateAnimation);
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
alphaAnimation.setDuration(500);
animationSet.addAnimation(alphaAnimation);
view.startAnimation(animationSet);

2. 使用ObjectAnimator创建属性动画


ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", 0, 100);
animator.setDuration(500);
animator.start();

六、Android弹窗确认

在弹窗中加入确认按钮可以让用户确认操作。


AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title")
        .setMessage("Message")
        .setIcon(R.mipmap.ic_launcher)
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 点击确定按钮时执行的操作
            }
        })
        .setNegativeButton("取消", null);
AlertDialog alertDialog = builder.create();
alertDialog.show();

七、Android弹窗警告提示框

在弹窗中加入警告提示框可以让用户知道操作可能带来的风险。


AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title")
        .setMessage("Message")
        .setIcon(R.mipmap.ic_launcher)
        .setPositiveButton("确定", null)
        .setNegativeButton("取消", null)
        .setNeutralButton("警告", null);
AlertDialog alertDialog = builder.create();
alertDialog.show();

八、Android弹窗延时确定按钮启用选取

在弹窗中加入延时确定按钮可以让用户在选取完成后再进行操作。


AlertDialog.Builder builder = new AlertDialog.Builder(this);
final AlertDialog alertDialog = builder.setTitle("Title")
        .setMessage("Message")
        .setIcon(R.mipmap.ic_launcher)
        .setPositiveButton("确定", null)
        .setNegativeButton("取消", null)
        .create();
alertDialog.show();

alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true);
    }
}, 5000);