| | |
| Stránka: 1 z 1
| [ Príspevkov: 2 ] | |
Autor | Správa |
---|
Registrovaný: 17.01.17 Prihlásený: 16.08.21 Príspevky: 12 Témy: 6 |
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á ...
|
|
Registrovaný: 17.01.17 Prihlásený: 16.08.21 Príspevky: 12 Témy: 6 | Napísal autor témy mackooo: 17.07.2017 11:22 | |
|
Vyriešené - čaká na stlačenie tlačidla. Doplnený handler: Kód: public static int getDialogValueBack() { final Handler handler = new Handler() { @Override public void handleMessage(Message mesg) { throw new RuntimeException(); } };
AlertDialog.Builder alert = new AlertDialog.Builder(ctx); alert.setTitle("Title"); alert.setMessage("Message"); alert.setPositiveButton("Return True", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { resultValue = 111; handler.sendMessage(handler.obtainMessage()); } }); alert.setNegativeButton("Return False", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { resultValue = 888; handler.sendMessage(handler.obtainMessage()); } }); alert.show();
try{ Looper.loop(); } catch(RuntimeException e){}
return resultValue; }
|
|
| Stránka: 1 z 1
| [ Príspevkov: 2 ] | |
Podobné témy | Témy | Odpovede | Zobrazenia | Posledný príspevok |
---|
| Ako pockat na Asynchronne funkcie. v JavaScript, VBScript, Ajax | 2 | 500 | 08.02.2017 8:59 doubleR | | Ebay - uznali reklamaciu, dali mi return shipping label, ako je to s postovnym? v Obchody, reklamácie a právo | 9 | 2132 | 29.03.2016 0:48 raf | | Ako flashnúť Android v Smartfóny a tablety | 15 | 2625 | 25.10.2011 15:00 brano282 | | Funkcia return vysledok; v Assembler, C, C++, Pascal, Java | 2 | 347 | 25.10.2014 10:37 dany2281995 | | return Unexpected token v JavaScript, VBScript, Ajax | 2 | 595 | 25.02.2012 18:45 iop | | Obchod Tenis Return v Obchody, reklamácie a právo | 2 | 751 | 03.07.2010 13:30 petKO | | curl return html v PHP, ASP | 4 | 408 | 16.10.2011 16:26 Dawn | | pomoc XMLHttpRequestObjekt RETURN v JavaScript, VBScript, Ajax | 20 | 1044 | 08.10.2009 10:07 pilná lama glama | | Ako zálohovať dáta ak nefunguje Android? v Smartfóny a tablety | 4 | 642 | 19.08.2017 15:25 Tomas-Lukca | | RETURN TO CASTLE WOLFENSTEIN v Počítačové hry | 18 | 1461 | 18.09.2006 19:20 Harlequin | | c++ main return exception v Assembler, C, C++, Pascal, Java | 4 | 405 | 12.07.2012 12:43 HT | | Android Kalendar - Ako nazdielat udalost / poslat pozvanku? v Smartfóny a tablety | 2 | 413 | 26.12.2021 11:26 Googler1 | | Návod ako uniesť lietadlo pomocou Android smartphonu v Novinky | 6 | 751 | 22.04.2013 21:22 Semp | | ako nahrať pôvodnú ROM LG O2X (Android) v Smartfóny a tablety | 0 | 346 | 02.06.2012 14:15 krrr | | return stringu po ajaxovom calle v JavaScript, VBScript, Ajax | 7 | 344 | 10.06.2014 22:57 hatto13 | | Prepisanie Return-Path v Postfixe v Operačné systémy Unix a Linux | 0 | 475 | 17.09.2016 12:31 JanoF |
| Nemôžete zakladať nové témy v tomto fóre Nemôžete odpovedať na témy v tomto fóre Nemôžete upravovať svoje príspevky v tomto fóre Nemôžete mazať svoje príspevky v tomto fóre
|
|