程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 編碼-做了一個android的登陸程序運行到ActivityManager:就結束了

編碼-做了一個android的登陸程序運行到ActivityManager:就結束了

編輯:編程解疑
做了一個android的登陸程序運行到ActivityManager:就結束了

manifest:
<?xml version="1.0" encoding="utf-8"?>
package="com.example.manage"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
            <uses-library android:name="android.test.runner" />
    <activity
        android:name="my"
        android:label="mine" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

mainactivity:

package com.example.manage;

import java.util.Map;

import com.example.manage.Utils;

import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{
private EditText etNumber;
private EditText etPassword;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
MapuserInfo=Utils.getUserInfo(this);
if(userInfo!=null){
etNumber.setText(userInfo.get("number"));
etPassword.setText(userInfo.get("password"));
}
}
private void initView(){
etNumber=(EditText) findViewById(R.id.et_number);
etPassword=(EditText) findViewById(R.id.et_password);
findViewById(R.id.btn_login).setOnClickListener((android.view.View.OnClickListener) this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
String number=etNumber.getText().toString().trim();
String password=etPassword.getText().toString();
if(TextUtils.isEmpty(number)){
Toast.makeText(this,"請輸入QQ號碼",Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(this,"請輸入密碼",Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "登陸成功", Toast.LENGTH_SHORT).show();
Log.i("MainActivity","記住密碼:"+number+","+password);
boolean isSaveSuccess=Utils.saveUserInfo(this,number,password);
if(isSaveSuccess){
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "保存失敗", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub

}

}

utils:

package com.example.manage;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Utils {
public static boolean saveUserInfo(Context context,String number,String password){
SharedPreferences sp=context.getSharedPreferences("data", Context.MODE_PRIVATE);
Editor edit=sp.edit();
edit.putString("username", number);
edit.putString("pwd",password);
edit.commit();
return true;
}
public static MapgetUserInfo(Context context) {
SharedPreferences sp=context.getSharedPreferences("data", Context.MODE_PRIVATE);
String number=sp.getString("userName", null);
String password=sp.getString("pwd",null);
MapuserMap=new HashMap();
userMap.put("number",number);
userMap.put("password", password);
return userMap;
}
}
這個是書上sharedpreferences的例子,不知道哪裡錯了,書上說utils類要創建在新的一個包裡面,不知道到放在哪就跟mainactivity放在一個裡面了。

最佳回答:


MainActivity.java

 package com.example.manage;

import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends  Activity implements OnClickListener{
    private EditText etNumber;
    private EditText etPassword;
    private Button etButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Map<String,String> userInfo=Utils.getUserInfo(this);
        if(userInfo!=null){
        etNumber.setText((String)userInfo.get("number"));
        etPassword.setText((String)userInfo.get("password"));
        }
    }

        private void initView(){
            etNumber=(EditText) findViewById(R.id.et_number);
            etPassword=(EditText) findViewById(R.id.et_password);
            etButton=(Button)findViewById(R.id.btn_login);
            etButton.setOnClickListener((android.view.View.OnClickListener) this);
            }

            public void onClick(View v) {
            // TODO Auto-generated method stub
            String number=etNumber.getText().toString().trim();
            String password=etPassword.getText().toString();
            if(TextUtils.isEmpty(number)){
            Toast.makeText(this,"請輸入QQ號碼",Toast.LENGTH_SHORT).show();
            return;
            }
            if(TextUtils.isEmpty(password)){
            Toast.makeText(this,"請輸入密碼",Toast.LENGTH_SHORT).show();
            return;
            }
            Toast.makeText(this, "登陸成功", Toast.LENGTH_SHORT).show();
            Log.i("MainActivity","記住密碼:"+number+","+password);
            boolean isSaveSuccess=Utils.saveUserInfo(this,number,password);
            if(isSaveSuccess){
            Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
            }
            else{
            Toast.makeText(this, "保存失敗", Toast.LENGTH_SHORT).show();
            }
            }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Utils.java

 package com.example.manage;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Utils {

    public static boolean saveUserInfo(Context context,String number,String password){
        SharedPreferences sp=context.getSharedPreferences("data", Context.MODE_PRIVATE);
        Editor edit=sp.edit();
        edit.putString("username", number);
        edit.putString("pwd",password);
        edit.commit();
        return true;
        }

        public static Map<String,String> getUserInfo(Context context) {
        SharedPreferences sp=context.getSharedPreferences("data", Context.MODE_PRIVATE);
        String number=sp.getString("userName", null);
        String password=sp.getString("pwd",null);
        Map<String,String> userMap=new HashMap<String,String>();
        userMap.put("number",number);
        userMap.put("password", password);
        return userMap;
        }

}

改為這樣試試

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved