[ Príspevkov: 2 ] 
AutorSpráva
Offline

Užívateľ
Užívateľ
Obrázok užívateľa

Registrovaný: 17.01.17
Prihlásený: 16.08.21
Príspevky: 12
Témy: 6 | 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:14

Hladal 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á ...


Offline

Užívateľ
Užívateľ
Obrázok užívateľa

Registrovaný: 17.01.17
Prihlásený: 16.08.21
Príspevky: 12
Témy: 6 | 6
Napísal autor témyOffline : 17.07.2017 11:22 | Android: AlertDialog - ako počkať na return?

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;
    }



 [ Príspevkov: 2 ] 


Android: AlertDialog - ako počkať na return?



Podobné témy

 Témy  Odpovede  Zobrazenia  Posledný príspevok 
V tomto fóre nie sú ďalšie neprečítané témy.

Ako pockat na Asynchronne funkcie.

v JavaScript, VBScript, Ajax

2

502

08.02.2017 8:59

doubleR

V tomto fóre nie sú ďalšie neprečítané témy.

Ebay - uznali reklamaciu, dali mi return shipping label, ako je to s postovnym?

v Obchody, reklamácie a právo

9

2145

29.03.2016 0:48

raf

V tomto fóre nie sú ďalšie neprečítané témy.

Ako flashnúť Android

v Smartfóny a tablety

15

2630

25.10.2011 15:00

brano282

V tomto fóre nie sú ďalšie neprečítané témy.

curl return html

v PHP, ASP

4

410

16.10.2011 16:26

Dawn

V tomto fóre nie sú ďalšie neprečítané témy.

pomoc XMLHttpRequestObjekt RETURN

v JavaScript, VBScript, Ajax

20

1045

08.10.2009 10:07

pilná lama glama

V tomto fóre nie sú ďalšie neprečítané témy.

Obchod Tenis Return

v Obchody, reklamácie a právo

2

753

03.07.2010 13:30

petKO

V tomto fóre nie sú ďalšie neprečítané témy.

Funkcia return vysledok;

v Assembler, C, C++, Pascal, Java

2

349

25.10.2014 10:37

dany2281995

V tomto fóre nie sú ďalšie neprečítané témy.

return Unexpected token

v JavaScript, VBScript, Ajax

2

597

25.02.2012 18:45

iop

V tomto fóre nie sú ďalšie neprečítané témy.

Ako zálohovať dáta ak nefunguje Android?

v Smartfóny a tablety

4

644

19.08.2017 15:25

Tomas-Lukca

V tomto fóre nie sú ďalšie neprečítané témy.

RETURN TO CASTLE WOLFENSTEIN

v Počítačové hry

18

1465

18.09.2006 19:20

Harlequin

V tomto fóre nie sú ďalšie neprečítané témy.

c++ main return exception

v Assembler, C, C++, Pascal, Java

4

408

12.07.2012 12:43

HT

V tomto fóre nie sú ďalšie neprečítané témy.

Android Kalendar - Ako nazdielat udalost / poslat pozvanku?

v Smartfóny a tablety

2

416

26.12.2021 11:26

Googler1

V tomto fóre nie sú ďalšie neprečítané témy.

Návod ako uniesť lietadlo pomocou Android smartphonu

v Novinky

6

753

22.04.2013 21:22

Semp

V tomto fóre nie sú ďalšie neprečítané témy.

ako nahrať pôvodnú ROM LG O2X (Android)

v Smartfóny a tablety

0

348

02.06.2012 14:15

krrr

V tomto fóre nie sú ďalšie neprečítané témy.

Prepisanie Return-Path v Postfixe

v Operačné systémy Unix a Linux

0

476

17.09.2016 12:31

JanoF

V tomto fóre nie sú ďalšie neprečítané témy.

return confirm() pred jQuery.load()

v JavaScript, VBScript, Ajax

2

456

13.10.2010 15:42

emer



© 2005 - 2024 PCforum, edited by JanoF