Jump to content

Edit Text vide


arsenedar

Recommended Posts

Bonjour j'aimerais savoir comment gérer le cas d'un edittext vide sachant que c'est un edittext qui demande un entier positif (android:numeric="integer")

Merci d'avance

package com.jdl.aiz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainViewActivity extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

  //On crée le Listener sur le Bouton
    OnClickListener ButtonConnexion = new OnClickListener()
    {
	 @Override
	 public void onClick(View actuelView)
	 {

	  EditText nbJoueur = (EditText)findViewById(R.acountcreation.password);
	  int nb= (int) Integer.parseInt(nbJoueur.getText().toString());
	  if(nb<=1)
	  {
	   Toast.makeText(getBaseContext(),"Deux joueurs au minimum!",Toast.LENGTH_SHORT).show();
		  return;
	  }
	 // On met en place le passage entre les deux activités sur ce Listener
	 Intent intent = new Intent(MainViewActivity.this,nom_joueur.class);
	 startActivity(intent);
	 }
    };

    //On récupere le bouton souhaité et on lui affecte le Listener
    Button bouton = (Button) findViewById(R.acountcreation.connect);
    bouton.setOnClickListener(ButtonConnexion);
   }
}

Link to comment
Share on other sites

En testant si le contenu de l'EditText est vide, avant de le convertir en Integer, non ?

String editTextStr = editText.getText().toString();
if ((editTextStr != null) && (editTextStr.trim().length() > 0)) {
 // Le contenu de l'EditText n'est pas vide
}

Link to comment
Share on other sites

Merci ça marche super bien :)

Petite question encore comment je fais passer mon entier nb (le nombre de joueur à mon activité suivante)

Avec un putextra il me semble mais je n'y arrive pas ..

ça plante.. Je vous met mon code:

package com.jdl.aiz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	setContentView(R.layout.main);

  //On crée le Listener sur le Bouton
	OnClickListener ButtonConnexion = new OnClickListener()
	{
	 @Override
	 public void onClick(View actuelView)
	 {

	  EditText nbJoueur = (EditText)findViewById(R.acountcreation.password);
	  String test = (nbJoueur.getText().toString());
	  if(test.length()==0)
	  {
	   Toast.makeText(getBaseContext(),"Trop soul pour taper le nombre de joueur?",Toast.LENGTH_SHORT).show();
  return;
	  }
	  int nb= (int) Integer.parseInt(nbJoueur.getText().toString());
	  if(nb<=1)
	  {
	   Toast.makeText(getBaseContext(),"Deux joueurs au minimum!",Toast.LENGTH_SHORT).show();
		  return;
	  }
	 // On met en place le passage entre les deux activités sur ce Listener
	 Intent intent = new Intent(MainViewActivity.this,nom_joueur.class);
	 intent.putExtra("nbjoueur",nb);
	 startActivity(intent);
	 }
	};

	//On récupere le bouton souhaité et on lui affecte le Listener
	Button bouton = (Button) findViewById(R.acountcreation.connect);
	bouton.setOnClickListener(ButtonConnexion);
}
}

et mon autre activité:

package com.jdl.aiz;

import android.app.Activity;
import android.os.Bundle;


public class nom_joueur extends Activity
{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
int nb = b.getInt("nbJoueur");
TextView tv = new TextView(this);
tv.setText(nb);
setContentView(tv);
return;
}
}

Merci pour tout :) :) :)

Edited by arsenedar
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...