Interface graphique
Résolu
jong_ye
Messages postés
20
Date d'inscription
Statut
Membre
Dernière intervention
-
jong_ye Messages postés 20 Date d'inscription Statut Membre Dernière intervention -
jong_ye Messages postés 20 Date d'inscription Statut Membre Dernière intervention -
bonjour ,j'essaie de créer une interface graphique avec android ,mais les widgets que je crée ne s'affiche pas sur l'émulateur ,et je ne comprend pas où se trouve le problème
voila le code que j'exécute:
et voila le fichier activity_main.xml
voila le code que j'exécute:
package com.example.carrent;
import android.os.Bundle;
import android.app.Activity;
import android.text.InputType;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView hello = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EditText editText = new EditText(this);
hello = new TextView(this);
hello.setText(R.string.hello_world);
hello.setPadding(15, 105, 21, 105);
setContentView(hello);
editText.setHint(R.string.editText);
editText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
editText.setLines(5);
Button button = new Button(this);
editText.setText(R.string.button);
}
}
et voila le fichier activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="25.7dp"
android:text="@string/hello_world" />
<EditText
android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/editText"
android:inputType="textMultiLine"
android:lines="5" />
</RelativeLayout>
A voir également:
- Interface graphique
- Changer carte graphique - Guide
- Graphique sparkline - Guide
- Graphique camembert excel - Guide
- Comment faire un graphique sur excel - Guide
- Ventilateur carte graphique ne tourne pas - Forum Carte graphique
4 réponses
Hello,
setContentView()te permet d'ajouter la vue à ton Activity. très souvent, on lui passes en paramètre le fichier de layout. De plus, inutile de recréer des TextView et EditText car ils sont déjà dans ton layout. Autant les récupérer avec
findViewById(). Du coup, voici ce que cela donne dans ton cas:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// attribution de la vue
setContentView(R.layout.activity_main);
// récupération des composants graphiques
EditText editText = findViexById(R.id.editText); //l'ID étant le même que dans le fichier xml
TextView hello = findViewById(R.id.text);
hello.setPadding(15, 105, 21, 105); // les paddings sont des entiers. Pas de virgules, donc
// manque juste le bouton....
}