我尝试下面的代码来设置edittext字段中的增量和减量值。但它不工作为什么?

时间:2012-07-07 06:06:39

标签: android numbers android-edittext increment decrement

package com.inc.increment;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Increment extends Activity implements OnClickListener {
int a,b;    
Button b1,b2;
EditText editText;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_increment);

    b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(this);
    b2 = (Button) findViewById(R.id.button2);
    b2.setOnClickListener(this);
        }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_increment, menu);
    return true;
}
    public void onClick(View v) {     

    // now perform the increment/decrement
    if (R.id.button1 == v.getId()) {

        editText = (EditText) findViewById(R.id.timepicker_input);
            a=Integer.parseInt(editText.getText().toString());
            b=a+1;
        String s1 = String.valueOf(b);
        editText.setText(s1);

    } else if (R.id.button2 == v.getId()) {

        editText = (EditText) findViewById(R.id.timepicker_input);
        int a=Integer.parseInt(editText.getText().toString());
            int b=a-1;
        String s1 = String.valueOf(b);
       editText.setText(s1);
    }       
  }
}

3 个答案:

答案 0 :(得分:1)

您是否为timepicker_input设置了默认值?如果在活动onCreate期间其空白,并且onclick将因为numberformatexception而失败,因为parseInt将失败...

你的错误是什么?

答案 1 :(得分:1)

请使用以下代码。

public class MainActivity extends Activity implements OnClickListener {

    Button b1, b2;
    EditText editText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editText = (EditText) findViewById(R.id.timepicker_input);
        b1 = (Button) findViewById(R.id.button1);
        b1.setOnClickListener(this);
        b2 = (Button) findViewById(R.id.button2);
        b2.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.options_menu, menu);
        return true;
    }

    public void onClick(View v) {

        // now perform the increment/decrement
        if (R.id.button1 == v.getId()) {

            if (editText.getText().toString().equals(null)
                    || editText.getText().toString().equals("")
                    || editText.getText().toString().equals("null")) {
                Toast.makeText(this, "Enter Some Value", Toast.LENGTH_SHORT)
                        .show();
            } else {
                int a = Integer.parseInt(editText.getText().toString());
                int b = a + 1;
                String s1 = String.valueOf(b);
                editText.setText(s1);
            }
        } else if (R.id.button2 == v.getId()) {
            if (editText.getText().toString().equals(null)
                    || editText.getText().toString().equals("")
                    || editText.getText().toString().equals("null")) {
                Toast.makeText(this, "Enter Some Value", Toast.LENGTH_SHORT)
                        .show();
            } else {
                int a = Integer.parseInt(editText.getText().toString());
                int b = a - 1;
                String s1 = String.valueOf(b);
                editText.setText(s1);
            }
        }
    }
}

答案 2 :(得分:0)

我对您的代码进行了一些更改,使用下面的代码,它将起作用。

package com.inc.increment;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Increment extends Activity implements OnClickListener {
int a,b;    
Button b1,b2;
EditText editText;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_increment);
   editText = (EditText) findViewById(R.id.timepicker_input);    
    b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(this);
    b2 = (Button) findViewById(R.id.button2);
    b2.setOnClickListener(this);

        }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_increment, menu);
    return true;
}
    public void onClick(View v) {     

    // now perform the increment/decrement
    if (R.id.button1 == v.getId()) {


            a=Integer.parseInt(editText.getText().toString());
            b=a+1;
        String s1 = String.valueOf(b);
        editText.setText(s1);

    } else if (R.id.button2 == v.getId()) {


         a=Integer.parseInt(editText.getText().toString());
             b=a-1;
        String s1 = String.valueOf(b);
       editText.setText(s1);
    }       
  }
}

不要在点击时创建edittext实例,它应该在oncreate中