2014-01-14 22 views
2

我試着喲實現一個計數器。將會有一個地方顯示計數和2個按鈕(++ & - )。在Android中實現一個簡單的計數器

<EditText 
    android:id="@+id/Edittext1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="text" 
    android:text="@string/cero" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/Edittext1" 
    android:layout_below="@+id/Edittext1" 
    android:layout_marginTop="16dp" 
    android:onClick="onClick" 
    android:text="@string/mas" /> 
<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/button1" 
    android:layout_toRightOf="@+id/button1" 
    android:layout_marginTop="16dp" 
    android:onClick="onClick" 
    android:text="@string/menos" /> 

但它不工作。我非常新的Android,我沒有看到我做錯了什麼。

public class MainActivity extends Activity implements OnClickListener { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    //View boton1 = findViewById(R.id.button1); 
    //boton1.setOnClickListener(this); 
    //View boton2 = findViewById(R.id.button2); 
    //boton2.setOnClickListener(this); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public void onClick(View v) { 
    TextView texto = (TextView) findViewById(R.id.Edittext1); 
    int n = Integer.parseInt(texto.getText().toString()); 
    switch(v.getId()) 
    { 
     case R.id.button1: texto.setText("case1"); 
      n++; 
      break; 
     case R.id.button2: texto.setText("case2"); 
      n--; 
      break; 
     default: texto.setText("default"); 
    } 
    texto.setText(n); 

} 

}

當我運行應用程序時,它突然停止(,意想不到的),當我點擊第二次。 任何人都可以給一點幫助嗎?謝謝你的時間!

+0

發佈logcat的 – Raghunandan

+0

http://androidbite.blogspot.co.il/2012/11/android-count-down-timer-example.html – Yanshof

回答

3
texto.setText(n); 

這是你的錯。 n是一個int值。 setText使用id查找資源。如果沒有找到,你會得到ResourceNotFoundException

您正在使用,而不是這個public final void setText (int resid)public final void setText (CharSequence text)

http://developer.android.com/reference/android/widget/TextView.html

更改爲

texto.setText(String.valueOf(n)); 

而且

EditText text0; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
texto = (EditText) findViewById(R.id.Edittext1); 

http://developer.android.com/reference/android/widget/TextView.html

雖然鑄造的EditText TextView的到是不是一個問題,我建議你換到texto = (EditText) findViewById(R.id.Edittext1);

沒有必要在按一下按鈕初始化的EditText每次。

同時刪除implements OnClickListener!因爲你android:onClick="onClick"

+0

非常感謝你!你幫了我很多!它現在完美運行! thanksssssss – iversoncru

+0

@iversoncru很樂意提供幫助 – Raghunandan