TQC-201 猜數字
這是TQC的201題,
程式碼為自行撰寫,
僅供參考。
主要練習字串拆解、EditText的addTextChangedListener事件
package COM.TQC.GDD02;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class GDD02 extends Activity {
/** Called when the activity is first created. */
Random gen=new Random();
private Button guessButton,renewButton;
private EditText guessText;
private TextView infoView,ansshow;
private String Answer = null;
private StringBuffer history = new StringBuffer();
private int Counter = 0;
private int Level = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findview();
setlistener();
doRenew(Level+3);
}
public void findview()
{
guessText = (EditText) findViewById(R.id.editText1);
infoView = (TextView)findViewById(R.id.textView2);
ansshow= (TextView)findViewById(R.id.textView3);
guessButton = (Button)findViewById(R.id.button1);
renewButton = (Button)findViewById(R.id.button2);
}
public void setlistener()
{
guessText.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
if(s.length()>0)
{
if (s.length() > 3)
{
guessText.setText("");
Toast popup = Toast.makeText(GDD02.this, "OVER 3 NUMBER", Toast.LENGTH_SHORT);
popup.show();
}
}
}
});
guessButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
doGuess();
}
});
renewButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
doRenew(Level+3);
}
});
}
private void doGuess(){
String retCheck;
retCheck = CheckAB();
history.append(++Counter + "." + guessText.getText().toString() + " : " + retCheck + "\n");
infoView.setText(history.toString());
if (retCheck.substring(0, 2).equals((Level + 3) + "A")){
infoView.setText("WINNER!!");
history.replace(0, history.length(), "");
doRenew(Level+3);
}
guessText.setText("");
}
private String CheckAB(){
String g = guessText.getText().toString();
int A = 0, B = 0;
for (int i = 0; i= 0) B++;
}
return A + "A" + B + "B";
}
private void doRenew(int dd){
StringBuilder sb = new StringBuilder();
boolean isRepeat = false;
Integer intTemp;
for (int i=0; i=0) isRepeat = true;
}while (isRepeat);
sb.append(intTemp);
}
Answer = sb.toString();
ansshow.setText(Answer);
Counter = 0;
guessText.setText("");
history = new StringBuffer();
}//
}
//
自我LV~