我的按钮什么都不做(点击我的按钮启动新活动)

时间:2013-05-12 22:45:33

标签: android

当我点击带有id" faouzi"的按钮时,它不起作用!有人能帮我吗? 我的目标是显示一个名为ChangePassword的活动,但它没有启动此活动&什么都不做..

    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 a 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 you entered is 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();
    }

}

以及布局的以下XML FILE:

<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>

和AndroidManifest文件:

<?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>

1 个答案:

答案 0 :(得分:2)

从xml中的faouzi按钮删除此行:

android:onClick="onClick"

您要么声明按钮有onClick并在XML中使用该公共方法,要么设置onClickListener,而不是两者都设置!