一、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);
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/247213.html