android - my button does nothing (launch new activity clicking in my button) -
when click in button id "faouzi", doesn't work! can me? target show activity named changepassword doesn't launch activity & nothing..
public class mainactivity extends activity { sharedpreferences pref; sharedpreferences prefs; sharedpreferences preferences; sharedpreferences.editor editor; @override protected void oncreate(bundle savedinstancestate) { pref = getsharedpreferences("mypassword",0); string password = pref.getstring("passwordretrieve", "8888"); super.oncreate(savedinstancestate); //supprimer la barre de titre de l'activité requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_main); //je change la couleur de fond du bouton en bleu button button = (button)findviewbyid(r.id.connection); //button.getbackground().setcolorfilter(color.blue, porterduff.mode.multiply); button.settext(password); //rendre le bouton "connection" fonctionnel button.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { edittext text = (edittext)findviewbyid(r.id.passwordedit); string str = text.gettext().tostring(); if(str.equals("1234")) { //on va vérifier si l'application déjà été lancée, si c'est le cas on saute certaines étapes d'initialisat° preferences = mainactivity.this.getsharedpreferences("nbrepet", mode_private); int value = preferences.getint("nbrepet", 0); if(value<1) { sendconnection(v); } else { intent intent = new intent(mainactivity.this, regulationtime.class); startactivity(intent); } } else { alertdialog alertdialog = new alertdialog.builder(mainactivity.this).create(); alertdialog.settitle("wrong password"); alertdialog.seticon(r.drawable.wrongpassword); alertdialog.setmessage("the password entered incorrect, please try again!"); alertdialog.show(); } } }); //lorsqu'on clique sur le bouton change password button changepassword = (button)findviewbyid(r.id.faouzi); changepassword.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(mainactivity.this, changepassword.class); startactivity(intent); } }); } //fonction appelée pour lancer l'activité "addmasternumber" public void sendconnection(view v) { intent intent = new intent(this, addrecipientnumber.class); startactivity(intent); //la nouvelle valeur de "nbrepet" devient 1 & nous empêche de relancer la série d'activités une 2nde fois //l'utilisateur ne doit procéder à certaines étapes 1seule fois (ex: entrer le n° de la socket); on évite cela! prefs = getsharedpreferences("nbrepet",context.mode_private); editor = prefs.edit(); editor.putint("nbrepet", 1); editor.commit(); } }
and following xml file layout:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_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/text1" android:layout_margintop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp" android:layout_alignleft="@+id/passwordedit" android:text="@string/connection" /> <textview android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/passwordedit" android:layout_below="@+id/text1" android:text="@string/password" android:layout_margintop="30dp" /> <edittext android:layout_height="wrap_content" android:layout_width="250dp" android:layout_centerhorizontal="true" android:layout_below="@+id/text2" android:id="@+id/passwordedit" android:inputtype="numberpassword" /> <button android:id="@+id/connection" android:layout_height="wrap_content" android:layout_width="250dp" android:layout_below="@+id/passwordedit" android:layout_margintop="10dp" android:layout_centerhorizontal="true" android:onclick="sendconnection" android:text="@string/connection" /> <textview android:id="@+id/defaultpassword" android:layout_alignleft="@+id/passwordedit" android:layout_width="fill_parent" android:layout_margintop="30dp" android:layout_height="wrap_content" android:text="@string/defaultpassword" android:layout_below="@+id/connection" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:text="@string/change" android:layout_below="@+id/defaultpassword" android:id="@+id/change" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:onclick="onclick" android:layout_below="@+id/change" android:id="@+id/faouzi" /> </relativelayout>
and here androidmanifest file:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.automatik" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="7" android:targetsdkversion="17" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.example.automatik.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name="com.example.automatik.addmasternumber" android:label="@string/title_activity_add_master_number" android:parentactivityname="com.example.automatik.mainactivity" > <meta-data android:name="android.support.parent_activity" android:value="com.example.automatik.mainactivity" /> </activity> <activity android:name="com.example.automatik.addrecipientnumber" android:label="@string/title_activity_add_recipient_number" android:parentactivityname="com.example.automatik.mainactivity" > <meta-data android:name="android.support.parent_activity" android:value="com.example.automatik.mainactivity" /> </activity> <activity android:name="com.example.automatik.regulationtime" android:label="@string/title_activity_regulation_time" android:parentactivityname="com.example.automatik.mainactivity" > <meta-data android:name="android.support.parent_activity" android:value="com.example.automatik.mainactivity" /> </activity> <activity android:name="com.example.automatik.changepassword" android:label="@string/title_activity_change_password" android:parentactivityname="com.example.automatik.mainactivity" > <meta-data android:name="android.support.parent_activity" android:value="com.example.automatik.mainactivity" /> </activity> </application> </manifest>
delete line faouzi
button in xml:
android:onclick="onclick"
you either declare button has onclick , use public method in xml or set onclicklistener, not both!
Comments
Post a Comment