【问题标题】:Re-using part of the code重用部分代码
【发布时间】:2017-04-24 16:47:48
【问题描述】:

我是一个真正的 Java 新手,尤其是这个面向对象的编程,所以我将不胜感激任何输入。

我已经制作了功能,可以更改每个评分栏选择的颜色,并且由于我想在其他评分栏上重复使用此功能,因此我尝试将整个代码插入方法中每个对象的名称和资源 id 作为参数,但我显然不知道我在做什么,因为我收到错误,因为名称变量已在范围中定义并且 findViewById 是非静态方法并从静态上下文中调用.

//rating bar
static void starLight(String name, int resId) {
    RatingBar name = (RatingBar) findViewById(resId);
    name.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float 
                rating, boolean fromUser) {
            int whole = Math.round(rating);

            Drawable progress = ratingBar.getProgressDrawable();
            if (whole == 1) {
                DrawableCompat.setTint(progress, 
                    ResourcesCompat.getColor(getResources(), R.color.colorGreen, null));
            }
            if (whole == 2) {
                DrawableCompat.setTint(progress, 
                    ResourcesCompat.getColor(getResources(), R.color.colorOrange, null));
            }
            if (whole == 3) {
                DrawableCompat.setTint(progress, 
                    ResourcesCompat.getColor(getResources(), R.color.colorRed, null));
            }
        }
    });
}

如果您能给我一些启发或指出正确的方向,我将不胜感激。

【问题讨论】:

  • 你有一个String name 并试图声明一个RatingBar name - 那么name 应该指哪个?
  • @UnholySheep 纠正我,因为我可能错了。如果我的布局文件中的评分栏很少,并且想要为每个评分栏添加自定义侦听器,我是否不必为通过 findViewById 链接的每个对象创建并赋予不同的名称?并将侦听器设置为每个?所以我想如果我传递不同的名称和正确的 id 作为参数,我将能够像这样初始化它们中的每一个。
  • 正如迈克尔在他的回答中指出的那样,您没有使用 String name 参数,因此您可以删除它 - 但是您不能有两个具有相同名称的不同变量(在同一范围内)

标签: java android class methods


【解决方案1】:

为什么不传入特定的 RatingBar 而不是 Resource id?然后您可以将方法签名保留为静态。正如其他人指出的那样,不需要String name参数。像这样:

static int green = ResourcesCompat.getColor(getResources(), R.color.colorGreen, null));


static void starLight(RatingBar ratingBar) {

 Drawable progress = ratingBar.getProgressDrawable();
        if (whole == 1) {
            DrawableCompat.setTint(progress, green);
...}

【讨论】:

  • 几乎成功了。 getResources 仍然会抛出错误。
  • 好的,将颜色定义为方法之外的整数(类级别变量),然后在方法中使用这些变量。
  • 谢谢@mondakuwar 成功了。我已经将这些颜色作为该方法的最终 int 参数传递了,它现在可以工作了。有什么东西在我的脑海里咔哒一声响起,但仍然感觉非常压倒性。您确实确实帮助了我,对此我非常感激。
【解决方案2】:

看来只需要改函数签名,去掉String参数即可:

static void starLight(/*String name,*/ int resId) {

我不知道你打算用它来做什么,但看起来你实际上并没有用它来做任何事情。

您收到编译器错误的原因是因为您有两个名为“name”的变量:一个作为参数,一个作为方法主体内的局部变量。


附带说明,您当前的版本重复了很多代码。你可以重构这个:

Drawable progress = ratingBar.getProgressDrawable();
if (whole == 1) {
    DrawableCompat.setTint(progress, 
        ResourcesCompat.getColor(getResources(), R.color.colorGreen, null));
}
if (whole == 2) {
    DrawableCompat.setTint(progress, 
        ResourcesCompat.getColor(getResources(), R.color.colorOrange, null));
}
// etc.

类似

Drawable progress = ratingBar.getProgressDrawable();
int colour;
switch (whole)
{
    case 1:
        colour = R.color.colorGreen;
        break;
    case 2:
        colour = R.color.colorOrange;
        break;
    case 3:
        colour = R.color.colorRed;
        break;
    default:
        colour = //something. Or throw an exception maybe?
}
DrawableCompat.setTint(progress, ResourcesCompat.getColor(getResources(), colour, null));

有趣的是,这确实比你现在得到的要长,但我认为更容易看到正在发生的事情,因为信息不那么密集。此外,如果您更改设置色调的方式,现在您只需在一处进行。

【讨论】:

  • 谢谢@Michael。您的代码看起来更整洁。对象和类对我来说仍然很困惑。我用 mysql 和 javascript 做了一些 php,但学习 java 和面向对象编程是另一回事。我现在做了很多训练,但仍然觉得这一切都过于复杂和尴尬,但我相信一旦我掌握了它,我会更喜欢它。
  • @aya9 没问题。都是练习。
猜你喜欢
  • 2011-05-10
  • 2013-05-17
  • 2015-06-14
  • 2014-10-21
  • 1970-01-01
  • 2012-11-15
  • 1970-01-01
  • 2016-12-23
  • 2017-10-29
相关资源
最近更新 更多