index.uts 2.4 KB
Newer Older
杜庆泉's avatar
杜庆泉 已提交
1 2 3 4 5 6 7 8 9 10

import {
	UTSAndroid
} from "io.dcloud.uts";

import Toast from 'android.widget.Toast';
import AlertDialog from 'android.app.AlertDialog';
import DialogInterface from 'android.content.DialogInterface';
import EditText from 'android.widget.EditText';

打打卡夫卡's avatar
打打卡夫卡 已提交
11
@Suppress("UNUSED_PARAMETER")
12 13
export function showAlert(_title: string|null, _message: string|null, _result: (index: Number) => void) {
	// todo
杜庆泉's avatar
杜庆泉 已提交
14 15
	let uiRunable = new DialogUIRunnable(null,_title!,_message!,"",false);
	UTSAndroid.getUniActivity()!.runOnUiThread(uiRunable)
16
}
杜庆泉's avatar
杜庆泉 已提交
17

打打卡夫卡's avatar
打打卡夫卡 已提交
18
@Suppress("UNUSED_PARAMETER")
杜庆泉's avatar
杜庆泉 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
export function showPrompt(_title: string|null,_message: string|null,_placeholder: string|null, success: (content: string)=>void) {
	let uiRunable = new DialogUIRunnable(success,_title!,_message!,_placeholder!,true);
	UTSAndroid.getUniActivity()!.runOnUiThread(uiRunable)
}

/**
 * 用户输入对话框监听器
 */
class DialogListener extends DialogInterface.OnClickListener{
	
	inputET:EditText
	callback:UTSCallback
	
	constructor(et:EditText,cb:UTSCallback){
		super();
		this.callback = cb;
		this.inputET = et;
	}
	
	override onClick(_dialog:DialogInterface,_arg1:Int ):void {
		//数据获取
		let input = this.inputET.getText().toString()
		this.callback(input);
		Toast.makeText(UTSAndroid.getUniActivity(),input,
				Toast.LENGTH_LONG).show();
				
	}
}


/**
 * Dialog ui任务封装
 */
class DialogUIRunnable extends Runnable {

	callback:UTSCallback|null = null
	title: string
	message: string
	placeholder: string
	needInput:boolean
	
	constructor(success?:UTSCallback,title: string,message: string,placeholder: string,needInput:boolean){
		super();
		if(success != null){
			this.callback = success
		}
		
		this.title = title
		this.message = message
		this.placeholder = placeholder
		this.needInput = needInput
	}

    override run():void {
		
		if(this.needInput){
			let et = new EditText(UTSAndroid.getUniActivity());
			et.setText(this.placeholder);
			
			new AlertDialog.Builder(UTSAndroid.getUniActivity())
					.setTitle(this.title)
					.setMessage(this.message)
					.setIcon(android.R.drawable.ic_dialog_info).setView(et)
					.setPositiveButton("确定", new DialogListener(et,this.callback!))
					.setNegativeButton("取消", null).show();
		}else{
			new AlertDialog.Builder(UTSAndroid.getUniActivity())
					.setTitle(this.title)
					.setMessage(this.message)
					.setIcon(android.R.drawable.ic_dialog_info)
					.setNegativeButton("取消", null).show();
		}
		

    }
};