Getting Data from EditText, RadioButton and Spinner - java

I search on many websites and many tutorials how can I get the data from editText and radioGroup and spinner DIRECTLY When the user input the data
I tried with this:
MyEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
{case1.setName(CaseName.getText().toString());
}
return true;
}
return false;
}
});
but it didn't take the data, I just use button to make that as shown below:
Test.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.cancel1:
try
{
// 1-Name
case1.setName(CaseName.getText().toString());
}catch(Exception e){
}
break;
}
}});
Can anyone explain what shall I use to save the data in Object(case1) directly when the user input it ?

editext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// s is your text .
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
})
for Radio button
checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks, depending on whether it's now checked
if (((CheckBox) v).isChecked()) {
Toast.makeText(HelloFormStuff.this, "Selected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(HelloFormStuff.this, "Not selected", Toast.LENGTH_SHORT).show();
}
}
});

Related

delete the contents of an edittext

I have 9 edittext and a button back I look when we click on the button back the content of a selected edittext is delete help me please
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
et1.addTextChangedListener(this);
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count == 0) {
//text was deleted.
}
}
}
});
back.setOnClickListener(new View.OnClickListener() {
if(TextUtils.isEmpty(et1.getText().toString()) {
//your first edit text is empty
}
});

Android EditText - how to detect if something is written by SoftKey?

I want to detect if something put into EditText was put there using SoftKey.
I've overridden dispatchKeyEvent but it doesn't work ...
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.e("key pressed", String.valueOf(event.getKeyCode()));
return super.dispatchKeyEvent(event);
}
Find the solution
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
// like this you can detect
}
return super.onKeyDown(keyCode, event);
}
yourEditText.addTextChangedListener(watcher);
private TextWatcher watcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(!s.toString().isEmpty()){
//your text is not empty
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
};

How to know if an EditText has changed before save the data?

I have a Profile's form with some EditText like the name, address, city and so on. And I have a Button to save this data once you press it.
So I would like to know how can I check if there have been changes before make my app connect with the database and update it.
I've thought using a TextChangedListener() with some boolean on the onTextChanged() or afterTextChanged(), but I don't know exactly how to do that.
Any suggestion? Thank you very much!
I´ve tried the code below and it works for me. I have this right now:
public class ProfileActivity extends Activity {
TextView done_btn;
EditText local_court;
boolean hasChanges = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
done_btn = (TextView) findViewById(R.id.done_profile_btn);
local_court = (EditText)findViewById(R.id.input_local_court);
done_btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
if(checkChanges()) {
//saveChanges();
finish();
Toast.makeText(getApplicationContext(), "Your changes have been saved", Toast.LENGTH_SHORT).show();
}
}
});
public boolean checkChanges() {
local_court.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
hasChanges = true;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
return hasChanges;
}
}
I guess I should make the same with the other EditText.
Thank you everyone!
If your goal is just to check if something has changed at all, just check the current EditText value to what was before.
But if you want to know if the text has been touched, even reverted back to original, do as below.
EditText etext = (EditText) view.findViewById(R.id.editTxt);
etext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s,
int start, int before,
int count) {
//Set your boolean here to true
}
#Override
public void beforeTextChanged(CharSequence s,
int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
}
);

How do I disable my Seekbar in Android with a Switch?

I want to disable my Seekbar and buttons with a switch which is Powerswitch
Here is a code snippet:
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
String sval = String.valueOf(i);
speakerVal.setText(sval);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
// button - Favorite Channel ABC
cabc = (Button) findViewById(R.id.buttonABC);
cabc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Set Favorite
curChannel.setText("007");
}
});
and here is my switch code:
final Switch switch1 = (Switch) findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(this);
more switch code:
public void onSwitchClicked(View view) {
final TextView PowerSwitch = (TextView) findViewById(R.id.PwrSwitchVal);
Switch sw = (Switch) view;
if (sw.isChecked()) {
PowerSwitch.setText("ON");
pwr = true;
} else {
PowerSwitch.setText("OFF");
pwr = false;
}
}
I know I can use the switch.isChecked(), but when I change the seekba1.setEnabled(false), I can't seem to get it to turn back on.
I haven't even tried to disable the buttons yet.
any help would be appreciated.
In your onSwitchClicked method, you should be calling seekbar.setEnabled in the if checked if-else statement. If checked seekbar.setEnabled(true) else seekbar.setEnabled(false)
try this.
switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0,
boolean arg1) {
if (arg1) {
seekbar1.setEnabled(true);
}
else{
seekbar1.setEnabled(false);
}
}
});

How do listen EditText?

I have a EditText. I wamt tp do something, when the user presses the Enter key while changing EditText. How can I do that?
The most simplest method:
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
sample code for text watcher
your_edittext.addTextChangedListener(new InputValidator());
private class InputValidator implements TextWatcher {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
}
}
First, create an OnEditorActionListener (as a private instance variable, for example):
private TextView.OnEditorActionListener mEnterListener =
new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_DOWN) {
/* If the action is a key-up event on the return key, do something */
}
return true;
});
Then, set the listener (i.e. in your onCreate method):
EditText mEditText = (EditText) findViewById(...);
mEditText.setOnEditorActionListener(mEnterListener);
Another alternative is:
your_edittext.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
You need to use TextWatcher for this purpose. Here is an example on how to work with TextWatcher and Android textwatcher API.
your_edittext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});

Categories

Resources