Dialog Toast Notification 사용

7608 단어
1. 일반 대화 상자 1
public void show(View v){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_launcher).setTitle("haha");
        builder.show();
    }
}

2. 일반 대화 상자 2
 public void show2(View v){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_launcher)
                .setTitle(" ?")
                .setMessage(" , ");
        OnClickListener clickListener=new OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                case DialogInterface.BUTTON_NEGATIVE:
                    Toast.makeText(MainActivity.this, 
                            "the chioce is no", Toast.LENGTH_SHORT).show();
                    break;
                case DialogInterface.BUTTON_POSITIVE:
                    Toast.makeText(MainActivity.this, 
                            "the chioce is yes", Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        };
        builder.setNegativeButton("no",clickListener);
        builder.setPositiveButton("yes",clickListener);
        builder.show();
    }

3. 간단한 목록 대화 상자
 public void show3(View v){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_launcher)
        .setTitle("what is your favorite?")
        .setItems(str, new OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,
                        "what your chioce is "+str[which], Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }

4. 라디오 리스트 대화 상자
 public void show4(View v){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        
        builder.setIcon(R.drawable.ic_launcher)
                .setTitle("learnHard")
                
                .setSingleChoiceItems(str, index, new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,
                                "what your chioce is"+str[which], 
                                Toast.LENGTH_SHORT).show();
                        index=which;
                        dialog.dismiss();
                    }
                });
        
        builder.show();
    }

5. 사용자 정의view
public void show6(View v){
        List<Map<String, Object>> data=new ArrayList<>();
        String[] names={"hah","xix","heh"};
        int[] images={R.drawable.q1,R.drawable.q2,R.drawable.q3};
        for (int i = 0; i < 3; i++) {
            Map<String, Object> map=new HashMap<>();
            map.put("name", names[i]);
            map.put("image", images[i]);
            data.add(map);
        }
        SimpleAdapter adapter=new SimpleAdapter(this, data,
                R.layout.item2, new String[]{"image","name"}, 
                new int[]{R.id.iv,R.id.tv});
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_launcher)
                .setTitle("just do it")
                .setAdapter(adapter, null);
        builder.show();    
    }

6. 다중 선택 목록 대화상자
public void show5(View v){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_launcher)
                .setTitle("show your chioce:")
                .setMultiChoiceItems(str, flag, new OnMultiChoiceClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, 
                            int which, boolean isChecked) {
                        if(isChecked){
                            flag[which]=true;
                        }else{
                            flag[which]=false;
                        }
                    }
                }).setPositiveButton("yes", new OnClickListener() {
                    String s="";
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        for (int i = 0; i < flag.length; i++) {
                            if(flag[i]){
                                s=s+str[i]+",";
                            }
                        }
                        s=s.substring(0,s.length()-1);
                        Toast.makeText(MainActivity.this,
                                "what your chioce is"+s, Toast.LENGTH_SHORT).show();
                    }
                });
        builder.show();
    }

7. 간단한 로그인 프로그램
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    
    // 
    public void show7(View v){
        final View v2=View.inflate(this, R.layout.item3, null);
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_launcher)
                .setTitle("chioce")
                .setView(v2)
                .setPositiveButton("login", new OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        EditText text1=(EditText) v2.findViewById(R.id.username);
                        EditText text2=(EditText) v2.findViewById(R.id.password);
                        String username=text1.getText().toString();
                        String passward=text2.getText().toString();
                        if(TextUtils.isEmpty(username)||TextUtils.isEmpty(passward)){
                            Toast.makeText(MainActivity.this, "null", Toast.LENGTH_SHORT).show();
                            mShow(dialog,false);
                            return;
                        }
                        if("zhang".equals(username)&&"123".equals(passward)){
                            Toast.makeText(MainActivity.this, "access", Toast.LENGTH_SHORT).show();
                            mShow(dialog,true);
                        }else{
                            Toast.makeText(MainActivity.this, "failure", Toast.LENGTH_SHORT).show();
                            mShow(dialog,false);
                        }
                    }
                })
                .setNegativeButton("dismiss", null);
        builder.show();
    }
    
    public static void mShow(DialogInterface dialog,boolean flag){
        Field field;
        try {
            field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
            field.setAccessible(true);
            field.set(dialog, flag);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
   String[] str={"sleeping","playing","learning","doing"};
    int  index=0;
    boolean[] flag={false,false,false,false};

좋은 웹페이지 즐겨찾기