代码改变世界

Android小试牛刀之1——对话框应用和SharedPeferences存储

2011-08-29 00:20  莫耶  阅读(335)  评论(0编辑  收藏  举报

我认为,再也没有比这更凌乱的代码了……

package com.locke.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class DataAdapeterActivity extends Activity {
	private static SharedPreferences settingActivity;
	static final int DIALOG_ALERT_ID = 0;
	static final int DIALOG_CONFIRM_ID = 1;
	static final int DIALOG_SHOW = 2;
	static final String KEY_NAME = "name";
	private Button buttonSave,buttonLoad;
	private Editor editor;
	private TextView textName;	
	private String inputName,message,storedName;
	
	/*构建对话框*/
	protected Dialog onCreateDialog(int id){
		Dialog dialog;
		switch(id){
			case DIALOG_ALERT_ID:
				dialog =  new AlertDialog.Builder(this)			
					.setMessage(message)
					.setPositiveButton(android.R.string.ok,new DialogInterface.OnClickListener() {						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();						
						}
					}).create();
				break;
			case DIALOG_CONFIRM_ID:
				dialog = new AlertDialog.Builder(this)
					.setMessage("确认覆盖吗名字?")
					.setPositiveButton(android.R.string.ok,new DialogInterface.OnClickListener() {						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							saveItem2Settings();
							dialog.dismiss();						
						}
					})
					.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					})
					.create();	
				break;
		case DIALOG_SHOW:			
			dialog = new AlertDialog.Builder(this)
				.setMessage(getName())
				.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();						
					}
				}).create();
			break;
		default:
			dialog =  null;
			break;
		}
		return dialog;
	}
	
    /* Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      
        
        settingActivity = getPreferences(MODE_PRIVATE);
        
        textName = (TextView)findViewById(R.id.text_name);        
        buttonSave = (Button)findViewById(R.id.button_save);
        buttonLoad = (Button)findViewById(R.id.button_get);
        
        /*保存按钮*/
        buttonSave.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {				
				inputName = textName.getText().toString();
				if(inputName==null || inputName=="" || inputName.length()==0){
					showDialog("请输入名字!");
					return;
				}
				if(settingActivity.contains(KEY_NAME)){
					showDialog(DIALOG_CONFIRM_ID);
					return;
				}			
				saveItem2Settings();
				showDialog("保存成功!");
			}
		});
        
        /*读取*/
        buttonLoad.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				showDialog(DIALOG_SHOW);
			}
		});
    }
    
    /*保存一对键值*/
    private void saveItem2Settings(){   
    	inputName = textName.getText().toString();
    	editor = settingActivity.edit();
    	editor.putString(KEY_NAME,inputName);
    	editor.commit();
    	
    	removeDialog(DIALOG_SHOW);
    }
    
    /*弹出消息*/
    private void showDialog(String msg){
    	message = msg;
    	showDialog(DIALOG_ALERT_ID);
    }
    
    /*获取名称*/
    private String getName(){
    	return settingActivity.getString(KEY_NAME,"无值");
    }
}