2015-02-23 79 views
-2

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

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

     } 
    }); 
+0

既然你对三个不同的'Button'做了完全相同的事情,为什么不写一个带'Button'参数并且调用它三次的方法呢? – ajb 2015-02-23 06:00:37

+0

对不起,我必须显示3个按钮,他们应该导航到三个不同的链接 – 2015-02-23 06:02:20

+1

然后,你不应该在你的代码示例中放置相同的链接(好吧,感谢编辑它)。无论如何,所有这一切意味着你为你的方法添加另一个参数来传递链接。 – ajb 2015-02-23 06:03:14

回答

0

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

以下是示例代码。

你的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 Variable作为全球

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); 
      } 
     }); 
    } 
+0

它为我工作。谢谢@CapDroid – 2015-02-23 07:02:08

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

    } 
0

OK,我会发布一个例子,但仅仅是因为有一个小非 - 需要解释的平凡概念:

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

     } 
    }); 
} 

详细信息是link将用于您为听众设置的匿名内部类中,link必须是final

我看过这个,并相信它会工作,但我没有尝试过。