Ahojte,
Robím malú aplikáciu, len je tam veľa dialógových okienok a preto som uniformoval do klasy.
Kód:
public class CX_Dialog {
public static Context source=null;
public int ret_value;
public CX_Dialog(Context source){
ret_value=2;
this.source=source;
}
public int oznam(String ozn, final int status){
final Object lock = new Object();
AlertDialog.Builder builder = new AlertDialog.Builder(source);
final TextView ozn_ = new TextView(source);
ozn_.setText(ozn);
final ImageView icon = new ImageView(source);
if(status==1 || status==2 || status==3){
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
set_ret_value(88);
dialog.cancel();
}
});
}
if(status==4){
builder.setPositiveButton("Áno", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
set_ret_value(1);
dialog.cancel();
}
});
builder.setNegativeButton("Nie", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
set_ret_value(0);
dialog.cancel();
}
});
}
if(status==1){
icon.setBackgroundResource(R.drawable.warn);
}
if(status==2){
icon.setBackgroundResource(R.drawable.success);
}
if(status==3){
icon.setBackgroundResource(R.drawable.info);
}
if(status==4){
icon.setBackgroundResource(R.drawable.que);
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(set_dp(60),set_dp(60));
icon.setLayoutParams(params);
ozn_.setGravity(Gravity.LEFT);
LinearLayout l1=new LinearLayout(source);
l1.setPadding(set_dp(12),set_dp(12),set_dp(12),set_dp(20));
l1.setOrientation(LinearLayout.HORIZONTAL);
l1.setGravity(Gravity.CENTER_VERTICAL);
ozn_.setPadding(set_dp(12),0,0,0);
l1.addView(icon);
l1.addView(ozn_);
builder.setView(l1);
final AlertDialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface arg0) {
if(status==1 || status==2 || status==3) { dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(0xffd56300);}
if(status==4) { dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(0xff00b300);
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(0xffd56300);
}
}
});
dialog.setCancelable(false); //backbutton
dialog.setCanceledOnTouchOutside(false); //klik mimo
dialog.show();
return get_ret_value();
}
public int set_dp(int dp){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dp,source.getResources().getDisplayMetrics());
}
public int get_ret_value(){
return this.ret_value;
}
public void set_ret_value(int new_value){
this.ret_value=new_value;
}
}
A použitie v activite:
Kód:
int i=99;
CX_Dialog cx_dialog = new CX_Dialog(LoginActivity.this);
i=cx_dialog.confirm("Nesprávne heslo alebo login","ds");
"print(i) => 99"
Chápem, prečo je premenná i nezmenená ... asynchrón, no ako to urobiť, aby to počkalo?
Skúšal som cez "synchronized", no bud som mal zlý manuál alebo neviem, nešlo to.
Viete mi niekto poradiť, aby pred return get_ret_value(); vlákno čakalo, než sa vykoná klik, resp. set_ret_value()?
Ďakujem Vám
// Spojený príspevok 14.07.2017 10:14Hladal som info ... Skúsil som použiť AsyncTask ...
Tu je kód .. a padá mi to ... priznám sa, nechápem významu a funkciam ...
onPre beží na UIthreade ako aj onPost ... onBackgrnd beží na inom vlákne ... ako mám počkať, aby mi to vrátilo hodnotu?
Kód:
class MyTask extends AsyncTask<Void,Integer,Integer> {
TextView ozn_=null;
AlertDialog dialog=null;
AlertDialog.Builder builder=null;
ImageView icon=null;
int ret=0;
@Override
protected void onPreExecute() {
super.onPreExecute();
ozn_ = new TextView(LoginActivity.this);
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
ImageView icon = new ImageView(LoginActivity.this);
icon.setBackgroundResource(R.drawable.warn);
builder.setMessage("oznam");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(set_dp(60), set_dp(60));
icon.setLayoutParams(params);
ozn_.setGravity(Gravity.LEFT);
LinearLayout l1 = new LinearLayout(LoginActivity.this);
l1.setPadding(set_dp(12), set_dp(12), set_dp(12), set_dp(20));
l1.setOrientation(LinearLayout.HORIZONTAL);
l1.setGravity(Gravity.CENTER_VERTICAL);
ozn_.setPadding(set_dp(12), 0, 0, 0);
l1.addView(icon);
l1.addView(ozn_);
builder.setView(l1);
builder.setPositiveButton("Áno", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
onProgressUpdate(11); //tu potrebujem yaynamenat hodnotu
dialog.dismiss();
}
});
builder.setNegativeButton("Nie", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
onProgressUpdate(22); //tu potrebujem yaynamenat hodnotu
dialog.dismiss();
}
});
dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface arg0) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(0xff00b300);
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(0xffd56300);
}
});
}
@Override
protected Integer doInBackground(Void... voids) {
dialog.setCancelable(false); //backbutton
dialog.setCanceledOnTouchOutside(false); //klik mimo
dialog.show();
return ret+=1;
}
@Override
protected void onProgressUpdate(Integer... values) {
ret=values[0];
}
@Override
protected void onPostExecute(Integer aVoid) {
Toast.makeText(LoginActivity.this, "ok"+ret, Toast.LENGTH_LONG).show();
}
}
pomože mi aspon s týmto niekto, ako mám donútiť, aby to počkalo na klik a až potom dobehlo a preskočilo naspäť na UIThread?
Nepadá mi to, ak prehodím .show() do onPre() .. no zbehne to a vypíše hned ten toast s hodnotou 1, čiže nečaká ...