2

我有 5 个 editTexts 和每个 editText 的 2 个按钮,所以有 10 个。每个都应该增加/减少特定的editText。现在我的代码完全可以工作,但正如您很快就会看到的那样,它效率不高,甚至不能确保应该包含分钟的 editTexts 的数字在 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();
        }
    });
}

}`

4

1 回答 1

0

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

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

标签

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

2)在你的xml中也使用onclick

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

在您的代码中:

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

如果您想以编程方式,使用上面的代码,获取相应的edittext,只需将它们的ID存储在arraylist中并使用int来获取相应的EditText。

于 2011-11-28T19:40:44.770 回答