简体   繁体   中英

how to avoid a starting activity when it's called

I'll try to explain it, I have two Activities, I'd create an activity with a button. When the button is pressed I don't want to the second activity starts, how can i do that? if you need some code tell me please!!

try this

if(yourtextfield1.getText().toString().length>0&&yourtextfield2.getText().toString().length>0&&yourtextfield3.getText().toString().length>0){

   //start your second activity here
}else {
   // give a warning to user
}
  1. You first make that button disabled enable=false .

Have a code like

private void validateButton()
{
    if(isAllDataEntered())
        ((Button)findViewById(R.id.btn_submit)).setEnabled(true);
}

Then you put something like this for all the editBoxes.

editUserName.setOnEditorActionListener(this);

and implement the methods and call it in onTextChanged method and You call this validateButton method.

In isAllDataEntered method, you can check if the Texts are entered.

Put a check Like.

EditText et1 = (EditText) findViewbyId(r.id.edittext1);
EditText et2 = (EditText) findViewbyId(r.id.edittext2);
EditText et3 = (EditText) findViewbyId(r.id.edittext3);

if(!(et1.getText().equals("") &&et2.getText().equals("") &&et3.getText().equals(""))){
   StartActivity();
}
else
{
alertUser();
}

where edittext1,edittext2,edittext3 is ur editext in xml

Set the launchMode of the activity in menifest to singleTop

    <activity
        android:name="MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM