重写代码以提高效率(按钮增量文本视图)

发布于 2024-12-18 13:34:26 字数 5579 浏览 4 评论 0原文

我有 5 个 editText,每个 editText 有 2 个按钮,所以有 10 个。每个都应该增加/减少特定的 editText。现在我的代码完全可以工作,但正如您很快就会看到的,它效率不高,甚至不能确保应该包含分钟的 editText 的数字在 0-59 之间(可以设置 n 分钟的时钟) 。无论如何,我只是想知道正确执行此操作的最佳或最有效的方法。

我的想法是有一个单独的方法,它接受参数,例如要递增/递减的特定 editText 以及是否添加或减去的另一个参数,但我不确定实现,因为 OnClickListener() 必须具有 public void onClick(查看v)方法。

谢谢!

代码:

package com.clock;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class addCourse extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addcourse);

    final Button save_Button = (Button) findViewById(R.id.addSave);
    final Button cancel_Button = (Button) findViewById(R.id.addCancel);
    final Button higherNumber_Button = (Button) findViewById(R.id.higherNumber_Button);
    final Button lowerNumber_Button = (Button) findViewById(R.id.lowerNumber_Button);
    final Button higherClasshh_Button = (Button) findViewById(R.id.higherClasshh_Button);
    final Button lowerClasshh_Button = (Button) findViewById(R.id.lowerClasshh_Button);
    final Button higherClassmm_Button = (Button) findViewById(R.id.higherClassmm_Button);
    final Button lowerClassmm_Button = (Button) findViewById(R.id.lowerClassmm_Button);
    final Button higherClockhh_Button = (Button) findViewById(R.id.higherClockhh_Button);
    final Button lowerClockhh_Button = (Button) findViewById(R.id.lowerClockhh_Button);
    final Button higherClockmm_Button = (Button) findViewById(R.id.higherClockmm_Button);
    final Button lowerClockmm_Button = (Button) findViewById(R.id.lowerClockmm_Button);
    final EditText courseCredits_Edit = (EditText) findViewById(R.id.courseCredits_Edit);
    final EditText hhClass_Edit = (EditText) findViewById(R.id.classHours_Edit);
    final EditText mmClass_Edit = (EditText) findViewById(R.id.classMins_Edit);
    final EditText hhClock_Edit = (EditText) findViewById(R.id.clockHours_Edit);
    final EditText mmClock_Edit = (EditText) findViewById(R.id.clockMins_Edit);

    higherNumber_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(courseCredits_Edit.getText().toString());
            ++a;
            courseCredits_Edit.setText(a + "");
        }
    });

    lowerNumber_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(courseCredits_Edit.getText().toString());
            --a;
            courseCredits_Edit.setText(a + "");
        }
    });

    higherClasshh_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(hhClass_Edit.getText().toString());
            ++a;
            hhClass_Edit.setText(a + "");
        }
    });

    lowerClasshh_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(hhClass_Edit.getText().toString());
            --a;
            hhClass_Edit.setText(a + "");
        }
    });

    higherClassmm_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(mmClass_Edit.getText().toString());
            ++a;
            mmClass_Edit.setText(a + "");
        }
    });

    lowerClassmm_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(mmClass_Edit.getText().toString());
            --a;
            mmClass_Edit.setText(a + "");
        }
    });

    higherClockhh_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(hhClock_Edit.getText().toString());
            ++a;
            hhClock_Edit.setText(a + "");
        }
    });

    lowerClockhh_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(hhClock_Edit.getText().toString());
            --a;
            hhClock_Edit.setText(a + "");
        }
    });

    higherClockmm_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(mmClock_Edit.getText().toString());
            ++a;
            mmClock_Edit.setText(a + "");
        }
    });

    lowerClockmm_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(mmClock_Edit.getText().toString());
            --a;
            mmClock_Edit.setText(a + "");
        }
    });

    save_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks

        }
    });

    cancel_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            finish();
        }
    });
}

}`

I have 5 editTexts and 2 buttons for each editText so 10 of them. Each one is supposed to increment/decrement a specific editText. Now my code works entirely but as you'll soon see, it is not efficient and doesn't even make sure the numbers are between 0-59 (clock that can be set for n minutes) for the editTexts that are supposed to contain minutes. Anyhow I just want to know the best or most-efficient way to properly do this.

My idea was to have a separate method that takes in parameters such as the specific editText to increment/decrement and another param for whether to add or subtract but I am not sure for the implementation since OnClickListener() has to have the public void onClick(View v) method.

Thanks!

Code:

package com.clock;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class addCourse extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addcourse);

    final Button save_Button = (Button) findViewById(R.id.addSave);
    final Button cancel_Button = (Button) findViewById(R.id.addCancel);
    final Button higherNumber_Button = (Button) findViewById(R.id.higherNumber_Button);
    final Button lowerNumber_Button = (Button) findViewById(R.id.lowerNumber_Button);
    final Button higherClasshh_Button = (Button) findViewById(R.id.higherClasshh_Button);
    final Button lowerClasshh_Button = (Button) findViewById(R.id.lowerClasshh_Button);
    final Button higherClassmm_Button = (Button) findViewById(R.id.higherClassmm_Button);
    final Button lowerClassmm_Button = (Button) findViewById(R.id.lowerClassmm_Button);
    final Button higherClockhh_Button = (Button) findViewById(R.id.higherClockhh_Button);
    final Button lowerClockhh_Button = (Button) findViewById(R.id.lowerClockhh_Button);
    final Button higherClockmm_Button = (Button) findViewById(R.id.higherClockmm_Button);
    final Button lowerClockmm_Button = (Button) findViewById(R.id.lowerClockmm_Button);
    final EditText courseCredits_Edit = (EditText) findViewById(R.id.courseCredits_Edit);
    final EditText hhClass_Edit = (EditText) findViewById(R.id.classHours_Edit);
    final EditText mmClass_Edit = (EditText) findViewById(R.id.classMins_Edit);
    final EditText hhClock_Edit = (EditText) findViewById(R.id.clockHours_Edit);
    final EditText mmClock_Edit = (EditText) findViewById(R.id.clockMins_Edit);

    higherNumber_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(courseCredits_Edit.getText().toString());
            ++a;
            courseCredits_Edit.setText(a + "");
        }
    });

    lowerNumber_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(courseCredits_Edit.getText().toString());
            --a;
            courseCredits_Edit.setText(a + "");
        }
    });

    higherClasshh_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(hhClass_Edit.getText().toString());
            ++a;
            hhClass_Edit.setText(a + "");
        }
    });

    lowerClasshh_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(hhClass_Edit.getText().toString());
            --a;
            hhClass_Edit.setText(a + "");
        }
    });

    higherClassmm_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(mmClass_Edit.getText().toString());
            ++a;
            mmClass_Edit.setText(a + "");
        }
    });

    lowerClassmm_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(mmClass_Edit.getText().toString());
            --a;
            mmClass_Edit.setText(a + "");
        }
    });

    higherClockhh_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(hhClock_Edit.getText().toString());
            ++a;
            hhClock_Edit.setText(a + "");
        }
    });

    lowerClockhh_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(hhClock_Edit.getText().toString());
            --a;
            hhClock_Edit.setText(a + "");
        }
    });

    higherClockmm_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(mmClock_Edit.getText().toString());
            ++a;
            mmClock_Edit.setText(a + "");
        }
    });

    lowerClockmm_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            int a = Integer.parseInt(mmClock_Edit.getText().toString());
            --a;
            mmClock_Edit.setText(a + "");
        }
    });

    save_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks

        }
    });

    cancel_Button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            finish();
        }
    });
}

}`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东北女汉子 2024-12-25 13:34:26

1) 在 xml 布局中使用每个按钮的“tag”属性。

http://developer.android.com/reference/android/view/View.html

标签

与 ID 不同,标签不用于识别视图。标签本质上是
可以与视图关联的额外信息。他们
最常用于方便地存储与视图相关的数据
视图本身,而不是将它们放在单独的
结构。

xml中也使用onclick

 <Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:tag="1"
     android:onClick="doSomething" />

2)在代码中的

public void doSomething(View v){
    int tag=v.getTag();
    //Do something smart with button, like odd numbers = plus and even = minus.
}

http://developer .android.com/reference/android/view/View.html#getTag()

如果你想以编程方式,使用上面的代码,获取相应的编辑文本,只需将它们的 ID 存储在数组列表中并使用 int即可得到对应的编辑文本。

1) use the "tag" property of each one of your button in your xml layout.

http://developer.android.com/reference/android/view/View.html

Tags

Unlike IDs, tags are not used to identify views. Tags are essentially
an extra piece of information that can be associated with a view. They
are most often used as a convenience to store data related to views in
the views themselves rather than by putting them in a separate
structure.

2) use also onclick in your xml

 <Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:tag="1"
     android:onClick="doSomething" />

in your code:

public void doSomething(View v){
    int tag=v.getTag();
    //Do something smart with button, like odd numbers = plus and even = minus.
}

http://developer.android.com/reference/android/view/View.html#getTag()

If you want to programmaticaly, with the code above, get the corresponding edittext, just store their ID in an arraylist and play with the int to get the corresponding EditText.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文