I want to perform many task in one Edittext. Like when I edit the first number in edittext it store in string value and edittext may be null after that when I enter 2nd number in same edittext 2nd number may also save in another string. Then after some action perform answer show in Textview.
Please help me out...
private void save_on_cick_listen(){
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(save == 0){
// save first string
editText.getText().clear(); //or you can use editText.setText("");
save+;
} else if (save == 1){
// save second string
editText.getText().clear(); //or you can use editText.setText("");
save+;
}
}
});
}
Related
I am working with validations in edit text. I have 5 edit text in my form and what I want is if the any of the edit text is null then it should not break its focus. I have tried verified answer from this link.
but in my case of 5 edit text its not working properly.
Please Help, Thanks.
Try this
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String val1=editText1.getText().toString().trim();
String val2=editText2.getText().toString().trim();
String val3=editText3.getText().toString().trim();
String val4=editText4.getText().toString().trim();
String val5=editText5.getText().toString().trim();
if(TextUtils.isEmpty(val5)){
editText5.requestFocus();
editText1.setFocusable(false);
editText2.setFocusable(false);
editText3.setFocusable(false);
editText4.setFocusable(false);
}else {
editText4.setFocusable(true);
}
if(TextUtils.isEmpty(val4)){
editText4.requestFocus();
editText1.setFocusable(false);
editText2.setFocusable(false);
editText3.setFocusable(false);
}else {
editText3.setFocusable(true);
editText1.setFocusable(false);
editText2.setFocusable(false);
editText4.setFocusable(false);
}
if(TextUtils.isEmpty(val3)){
editText3.requestFocus();
editText1.setFocusable(false);
editText3.setFocusable(false);
editText4.setFocusable(false);
}else {
editText2.setFocusable(true);
}
if(TextUtils.isEmpty(val2)){
editText2.requestFocus();
}else {
editText1.setFocusable(true);
}
if(TextUtils.isEmpty(val1)){
editText1.requestFocus();
}
if(!TextUtils.isEmpty(val1) &&
!TextUtils.isEmpty(val2) &&
!TextUtils.isEmpty(val3) &&
!TextUtils.isEmpty(val4) &&
!TextUtils.isEmpty(val5) ){
Toast.makeText(MainActivity.this, "PASS", Toast.LENGTH_SHORT).show();
}
}
});
First check EditText empty,null or matches to some specific text
based on your requirement.
Check 5 EditTexts using if & else if statements. if a condition is true, change things to EditText which
you want to do.
In else part, set things you want to set if, if or else if conditions false.
According to my understanding in your question, I wrote a code. try it.
final EditText et1 = (EditText) findViewById(R.id.et1); //get referance to all 5 EditTexts. I showed for 2 EditTexts
final EditText et2 = (EditText) findViewById(R.id.et2);
if (et1.getText().toString().length() < 1) { //check EditText empty or not.
//do what you want to edit/change in EditText. like below.
et1.setFocusableInTouchMode(true);
et1.requestFocus();
} else if (et2.getText().toString().length() < 1) {
//Check for second EditText empty or not. Do this for all 5 edit texts.
et2.setFocusableInTouchMode(true);
et2.requestFocus();
}
else{
//if All EditTexts not empty. do what you want to edit/change in edittext.
et1.setFocusableInTouchMode(false);
et2.setFocusableInTouchMode(false);
}
Button btnSearch = (Button) pnlView.findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new OnClickListener() {#
Override
public void onClick(View v) {
results.clear();
if (txtPatientID.getText().equals("") && txtFirstName.getText().equals("")
&& txtLastName.getText().equals("") && txtDOB.getText().equals("")
&& cboGender.getSelectedItem().equals("")) {
Toast.makeText(getActivity(),
"No criteria added", Toast.LENGTH_LONG).show();
return;
}
new Async().execute();
}
});
I have four text field and a spinner, when the user clicks search button, i need to display a message saying no criteria added. Its working, but i am not sure whether i am following the right approach in checking for empty value in textfield and spinner.
You should check the length instead of comparing with empty value. Because it will fail in one or more cases. you should check like this
if(txtPatientID.getText().toString().trim().length()==0)
trim() method will remove one or more spaces in your text
This seems okay. But you can trim() the getText() results to handle empty space characters.
My intention is to put an Edittext, and a Button below, and when the user put an specific word and click the button, the value is correct, and all the other different words are wrong.. How to identify it with codes?
btn.onClickListiner(this);
#Override
public void onClick(View v) {
if (v == btn) {
// check word
String word = editText.getText().toString(); // don't forget "toString()" .
// checking word below :
}}
I am currently creating an android application with different options. One of the option would be to have a button that would show "Activate" as default. When the application would be running, clicking on it would change it to "Disable" and then to "activate" if clicked again. I believe that all I have to do is to .getText with a string variable then use this variable in a if statement but it seems like it is not reacting to any of my conditions...
final Button button = (Button) findViewById(R.id.bSensor);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String buttonText = button.getText().toString();
if (buttonText == "#string/Disable") {
button.setText(R.string.Enable);
}
else if (buttonText == "#string/Enable"){
button.setText(R.string.Disable);
}
}
});
Thanks for help
Phyzikk
You shouldn't use the == operator when comparing strings in Java. Source
You should either use the .equals() method of the string, or alternatively you could keep a global boolean state flag to determine which value is set. This way you won't need to do a string compare every time you need to figure out if it's active or disabled.
Use .equals to compare strings. You wont need the #String/ prefix as this is not part of what the button displays.
final Button button = (Button) findViewById(R.id.bSensor);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
String buttonText = button.getText().toString();
if (buttonText.equals(getResources().getText(R.string.Disable)) {
button.setText(R.string.Enable);
}
else if (buttonText.equals(getResources().getText(R.string.Enable)){
button.setText(R.string.Disable);
}
}
});
I'm making an app for Android, and if you click the calculate button on one of the pages, without anything entered in the text boxes, it force closes. Some users were wondering if this could be fixed, so I was wondering if there was a way to make the onClickListener execute only if there is something inside the EditText.
You have to check it yourseft, such as:
final EditText editText = ...; // your edit
// check in your onClickListener
if (editText.getText().toString().isEmpty){ // Check if your EditText is
}else{ // If your EditTexit is not null
}
Please, search google before asking any question!
You can try following code,
suppose you have a EditText txtNum1 & txtNum2 , so onClickListener() method you can write following condition
public void onClick(View v)
{
if ( v == cmdCalculate )
{
if ( !txtNum1.getText().equals("") && !txtNum2.getText().equals("") )
{
// your calculation code
}
else
{
// post error msg code
}
}
}