09.02.2017 Views

creez-des-applications-pour-android

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

3 Création d’interfaces graphiques<br />

public void init() {<br />

// Paramètres utilisés <strong>pour</strong> indiquer la taille voulue <strong>pour</strong> la vue<br />

int wrap = LayoutParams.WRAP_CONTENT ;<br />

int fill = LayoutParams.FILL_PARENT ;<br />

// On veut que notre layout soit de haut en bas<br />

setOrientation(LinearLayout.VERTICAL);<br />

// Et qu'il remplisse tout le parent.<br />

setLayoutParams(new LayoutParams(fill, fill));<br />

// On construit les widgets qui sont dans notre vue<br />

mEdit = new EditText(getContext());<br />

// Le texte sera de type web et peut être long<br />

mEdit.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT | InputType.TYPE_TEX<br />

// Il fera au maximum dix lignes<br />

mEdit.setMaxLines(10);<br />

// On interdit le scrolling horizontal <strong>pour</strong> <strong>des</strong> questions de confort<br />

mEdit.setHorizontallyScrolling(false);<br />

// Listener qui se déclenche dès que le texte dans l'EditText change<br />

mEdit.addTextChangedListener(new TextWatcher() {<br />

// À chaque fois que le texte est édité<br />

@Override<br />

public void onTextChanged(CharSequence s, int start, int before, int count) {<br />

// On change le texte en Spanned <strong>pour</strong> que les balises soient interprétées<br />

mText.setText(Html.fromHtml(s.toString()));<br />

}<br />

// Après que le texte a été édité<br />

@Override<br />

public void beforeTextChanged(CharSequence s, int start, int count, int after) {<br />

}<br />

// Après que le texte a été édité<br />

@Override<br />

public void afterTextChanged(Editable s) {<br />

}<br />

});<br />

mText = new TextView(getContext());<br />

mText.setText("");<br />

// Puis on rajoute les deux widgets à notre vue<br />

addView(mEdit, new LinearLayout.LayoutParams(fill, wrap));<br />

228

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!