如何在java代码下面减少这个?

时间:2015-02-23 05:57:07

标签: java android

在这里,我编写了一些代码,使用android中的不同按钮导航到不同的链接。无论如何还要比我编码的代码进一步减少代码。如果有可能请帮我减少代码。这是我的代码:

Button tv = (Button) findViewById(R.id.my1);
    tv.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            v.startAnimation(myscale);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        Intent intent = new Intent(
                                Intent.ACTION_VIEW,
                                Uri.parse("link1"));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    } catch (Exception e) {
                        Log.e("Exception Caught", e.toString());
                    }

                }
            }, 50);

        }
    });

    Button tv1 = (Button) findViewById(R.id.my2);
    tv1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            v.startAnimation(myscale);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        Intent intent = new Intent(
                                Intent.ACTION_VIEW,
                                Uri.parse("link2"));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    } catch (Exception e) {
                        Log.e("Exception Caught", e.toString());
                    }

                }
            }, 50);

        }
    });
    Button tv2 = (Button) findViewById(R.id.my3);
    tv2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            v.startAnimation(myscale);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        Intent intent = new Intent(
                                Intent.ACTION_VIEW,
                                Uri.parse("link3"));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    } catch (Exception e) {
                        Log.e("Exception Caught", e.toString());
                    }

                }
            }, 50);

        }
    });

5 个答案:

答案 0 :(得分:2)

一旦尝试如下,请采用可重用性的方法

public void openLink(final String link){
 v.startAnimation(myscale);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    Intent intent = new Intent(
                            Intent.ACTION_VIEW,
                            Uri.parse(link));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                } catch (Exception e) {
                    Log.e("Exception Caught", e.toString());
                }

            }
        }, 50);
}

并实施 onClickListener onClick

@Override
public void onClick(View v) {
    super.onClick(v);
    switch (v.getId()) {    
    case R.id.my1:
       openLink(link1);
        break;
    case R.id.my2:
        openLink(link2);
        break;
   case R.id.my3:
          openLink(link3);
        break;
    }

希望这会对你有所帮助。

答案 1 :(得分:1)

   Button tv = (Button) findViewById(R.id.my1);
    tv.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startAnotherActivity("link1");
        }
    });

    Button tv1 = (Button) findViewById(R.id.my2);
    tv1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startAnotherActivity("link2");
        }
    });
    Button tv2 = (Button) findViewById(R.id.my3);
    tv2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startAnotherActivity("link3");

        }
    });

    private void startAnotherActivity(final String link){
        v.startAnimation(myscale);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    Intent intent = new Intent(
                            Intent.ACTION_VIEW,
                            Uri.parse(link));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                } catch (Exception e) {
                    Log.e("Exception Caught", e.toString());
                }

            }
        }, 50);

    }

答案 2 :(得分:0)

在您的活动上实现onClickListener并覆盖onClick()方法并以此方式编写代码

@Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {    
        case R.id.my1:
            //your code here
            break;
        case R.id.my2:
            //your code here
            break;
        }

答案 3 :(得分:0)

为click侦听器创建一个方法,并在条件时使用。

以下是示例代码。

您的OnCreate代码:

Button tv = (Button) findViewById(R.id.my1);
onClick(tv);

tv1 = (Button) findViewById(R.id.my2);
onClick(tv1);

Button tv2 = (Button) findViewById(R.id.my3);
onClick(tv2);

常用方法:将mLink变量声明为全局

public void onClick(View v)
    {
        v.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(v == tv)
                    mLink = "link1";
                else if(v == tv1)
                    mLink = "link2";
                else if(v == tv2)
                    mLink = "link3";

                v.startAnimation(myscale);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(mLink));
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        } catch (Exception e) {
                            Log.e("Exception Caught", e.toString());
                        }
                    }
                }, 50);
            }
        });
    }

答案 4 :(得分:0)

好的,我会发布一个例子,但这只是因为有一些非平凡的概念需要解释:

Button tv = (Button) findViewById(R.id.my1);
setListenerForButton(tv, "link1");
Button tv1 = (Button) findViewById(R.id.my2);
setListenerForButton(tv1, "link2");
Button tv2 = (Button) findViewById(R.id.my3);
setListenerForButton(tv1, "link3");



private void setListenerForButton(Button button, final String link) {
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            v.startAnimation(myscale);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        Intent intent = new Intent(
                                Intent.ACTION_VIEW,
                                Uri.parse(link));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    } catch (Exception e) {
                        Log.e("Exception Caught", e.toString());
                    }

                }
            }, 50);

        }
    });
}

详细信息是,要为您为侦听器设置的匿名内部类中使用linklink必须为final

我看了过来并相信它会奏效,但我还没试过。