java 安卓开关/案例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12213383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:03:41  来源:igfitidea点击:

Android Switch/case

javaandroidswitch-statementcase

提问by Aldridge1991

I've got this switch/case structure:

我有这个开关/案例结构:

public void onClick(View arg0) {

    // TODO Auto-generated method stub
    switch(arg0.getId()){

        case R.id.save:

            if (et.getText() !=null && thumbnail != null){

                 TableRow tr = new TableRow(this);
                 ImageView view = new ImageView(this);
                 TextView view2 = new TextView(this);
                 Button view3 = new Button(this);
                 view3.setOnClickListener(this);

                 titulo = new String[500];
                 mensaje = new String[500];
                 fotos = new Bitmap[500];

                 view3.setId(i);


                 view.setImageBitmap(thumbnail);
                 view.setPadding(1, 5, 0, 0);
                 view.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

                 Calendar c = Calendar.getInstance();
                 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");

                 titulo[i]=""+et.getText();
                 mensaje[i]=""+et1.getText();
                 fotos[i]=thumbnail;
                 i++;

                 view2.setText("" + et.getText()  + dateFormat.format(c.getTime()) );
                 view2.setGravity(Gravity.CENTER_HORIZONTAL);
                 view2.setGravity(Gravity.CENTER_VERTICAL);

                 view3.setGravity(Gravity.RIGHT);
                 view3.setGravity(Gravity.CENTER_VERTICAL);

                 DisplayMetrics metrics = new DisplayMetrics();
                 getWindowManager().getDefaultDisplay().getMetrics(metrics);

                 tr.addView(view, metrics.widthPixels/3, 150);
                 tr.addView(view2, metrics.widthPixels/2, 100);
                 tr.addView(view3, metrics.widthPixels/6, 20);
                 tl.addView (tr, 0);


                 final Toast toastMensaje = Toast.makeText(getApplicationContext(),
                            "Tu entrada se cargó correctamente", Toast.LENGTH_LONG);
                    toastMensaje.setGravity(Gravity.CENTER, 0, 0);
                    toastMensaje.show();







                    et.setText("");
                    et1.setText("");
                    i1.setVisibility(View.GONE);


            }

            else{

                final Toast toastMensaje = Toast.makeText(getApplicationContext(),
                        "Tienes que a?adir un título y una foto", Toast.LENGTH_LONG);
                toastMensaje.setGravity(Gravity.CENTER, 0, 0);
                toastMensaje.show();

            }


        break;

        case R.id.photo:

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);


        break;

        case R.id.gallery:

            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
             startActivityForResult(intent, TFRequestCodes);

        break;

        case 0:

        final Toast toastMensaje = Toast.makeText(getApplicationContext(),
                   titulo[0], Toast.LENGTH_LONG);
            toastMensaje.setGravity(Gravity.CENTER, 0, 0);
            toastMensaje.show();

            break;





    }

}

It works fine but i have to repeat the code for a long ammount of numbers from 0 to a number which depends on an array.lenght

它工作正常,但我必须为从 0 到取决于 array.lenght 的数字的大量数字重复代码

Is there a way to reduce all that code?

有没有办法减少所有这些代码?

Maybe a for loop, i've tried it but i cant make it work.

也许是一个 for 循环,我试过了,但我不能让它工作。

Thanks

谢谢

回答by assylias

You can replace your case 0and following with:

您可以将您的case 0和以下内容替换为:

    default:
        if (arg0.getId() < titulo.lenth) {
            final Toast toastMensaje = getToast(arg0.getId());
            toastMensaje.setGravity(Gravity.CENTER, 0, 0);
            toastMensaje.show();
        } else {
            //not a valid value
        }
        break;
}

private Toast getToast(int i) {
    return Toast.makeText(getApplicationContext(), titulo[i], Toast.LENGTH_LONG);
}