Reputation: 137
I want: if user already put name and choose game types, user can click button 'OK' but if user doesn't already put name and choose game types, user can't click button 'OK'.
public class UserConfig extends Activity {
Button userConfigBack;
Button userConfigOK;
EditText userNameTextbox;
Spinner gameTypes;
private String gameType;
private String playerName;
private Handler handler;
Thread validation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_config);
userConfigBack = (Button) findViewById(R.id.user_config_back);
userConfigOK = (Button) findViewById(R.id.user_config_ok);
userNameTextbox = (EditText) findViewById(R.id.user_name_textbox);
gameTypes = (Spinner) findViewById(R.id.game_types);
gameTimes = (Spinner) findViewById(R.id.game_times);
userConfigOK.setEnabled(false);
userConfigOK.setClickable(false);
setName();
setGameType();
handler = new Handler();
autoValidation();
OK();
}
public void setName() {
playerName = userNameTextbox.getText().toString();
userNameTextbox.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
playerName = userNameTextbox.getText().toString();
nameLabel.setText(playerName);
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
}
public void setGameType() {
gameTypes.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View arg1,
int arg2, long arg3) {
gameType = (String) gameTypes.getSelectedItem();
gameTypeLabel.setText(gameType);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
public void OK() {
userConfigOK.setOnClickListener( new OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(UserConfig.this, EnemyConfig1.class);
startActivity(intent);
}
});
}
public void autoValidation() {
validation = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
if(!gameType.equals("")) {
if(!playerName.equals("")) {
userConfigOK.setEnabled(true);
userConfigOK.setClickable(true);
}
}
}
});
}
});
validation.start();
}
}
I create thread inside method AUTOVALIDATION. If editText not null and user already choose game types spinner, user can click button OK. But
userConfigOK.setEnabled(true);
userConfigOK.setClickable(true);
doesn't work.
Upvotes: 0
Views: 42
Reputation: 26
You can't change any UI elements from another Thread. Try to use a runOnUiThread
this should solve the problem.
You really should read this carefully: http://developer.android.com/guide/components/processes-and-threads.html This will save you al lot of time if you are working with Threads.
And here is an example that helped me a lot: how to use runOnUiThread
Upvotes: 1
Reputation: 917
add a text watcher
to your edittext and then use this if elsestatement:
if(!userNameTextbox.getText().toString().equals("")){
//do your job
}else{
//Toast some thing
}
Upvotes: 0