13、携帯電話の盗難防止--ガイドの設定が完了した画面

11277 ワード

layoutレイアウトファイル:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView style="@style/title_center_text"
        android:text="    "/>
    <View style="@style/splitter_view"/>
    <RelativeLayout style="@style/relativelayout">
        <TextView style="@style/content_text"
            android:layout_alignParentLeft="true"
            android:text="    :"/>
        <TextView style="@style/content_text"
            android:id="@+id/tv_lost_safe_number"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>
    <View style="@style/splitter_view"
        android:background="@drawable/listview_devider"/>

    <RelativeLayout style="@style/relativelayout"
        android:gravity="center_vertical">
            <TextView style="@style/content_text"
                android:layout_centerVertical="true"
                android:text="      "/>
        <CheckBox style="@style/content_text"
            android:id="@+id/cb_lost_protect_setting"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>
    <View style="@style/splitter_view"
        android:background="@drawable/listview_devider"/>

    <TextView style="@style/content_text"
        android:id="@+id/tv_lost_protect_resetup"
        android:text="        "/>
    <View style="@style/splitter_view"
        android:background="@drawable/listview_devider"/>

    <TextView style="@style/content_text"
        android:text="      :
#*location*#
#*alarm*#
#*lockscreen*#
#*wipedata*# "/> </LinearLayout>

ここで「防犯保護設定」のTextViewはCheckBoxと同じレイアウト内にありますが、CheckBoxの高さが高いため、TextViewが縦方向に中央に表示されるように設定する必要がありますので、設定する必要があります.
android:layout_centerVertical="true"

コード:
package com.example.mobilesafe;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.utils.util;

/**
 * Created by sing on 13-12-25.
 * desc:
 */
public class LostProtectedActivity extends Activity implements View.OnClickListener {

    //    
    public static final int MENU_ITEM_ID_RENAME_TITLE = Menu.FIRST + 1;

    //    
    private SharedPreferences sp;

    private AlertDialog dialog;

    //                     
    private EditText et_first_dlg_pswd;
    private EditText et_first_dlg_pswd_confirm;
    private Button bt_first_dlg_ok;
    private Button bt_first_dlg_cancel;

    //                      
    private EditText et_normal_dlg_pswd;
    private Button bt_normal_dlg_ok;
    private Button bt_normal_dlg_cancel;

    private TextView tv_lost_safe_number;
    private CheckBox cb_lost_protect_setting;
    private TextView tv_lost_protect_resetup;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        sp = getSharedPreferences("config", MODE_PRIVATE);

        /*    ,        
        //                      ,           
        if (isFirstEntry()) {
            showFirstEntryDialog();
        }else {
            showNormalEntryDialog();
        }
        */

        //            ,       
        if (isFirstSetup()) {
            Intent intent = new Intent(this, LostProtectStep1Activity.class);
            startActivity(intent);
        } else {
            //     ,        
            setContentView(R.layout.activity_lostprotected);
            tv_lost_safe_number = (TextView) findViewById(R.id.tv_lost_safe_number);
            cb_lost_protect_setting = (CheckBox) findViewById(R.id.cb_lost_protect_setting);
            tv_lost_protect_resetup = (TextView) findViewById(R.id.tv_lost_protect_resetup);

            String safenumber = sp.getString("safenumber", "");
            tv_lost_safe_number.setText(safenumber.isEmpty()?"        ":safenumber);

            boolean protecting = sp.getBoolean("protecting", false);
            cb_lost_protect_setting.setChecked(protecting);
            cb_lost_protect_setting.setText(protecting?"        ":"        ");
            cb_lost_protect_setting.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    cb_lost_protect_setting.setChecked(b);
                    cb_lost_protect_setting.setText(b?"        ":"        ");
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putBoolean("protecting", b);
                    editor.commit();
                }
            });

            tv_lost_protect_resetup.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(LostProtectedActivity.this, LostProtectStep1Activity.class);
                    startActivity(intent);
                    finish();
                }
            });
        }
    }

    /**
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(android.view.Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(Menu.NONE, MENU_ITEM_ID_RENAME_TITLE, 0, "      ");
        return true;
    }

    /**
     *       
     *
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(android.view.MenuItem item) {
        //  “      ”  ,            
        if (item.getItemId() == MENU_ITEM_ID_RENAME_TITLE) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            final EditText et = new EditText(this);
            et.setHint("        ,   ");
            builder.setView(et);
            builder.setPositiveButton("  ", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    String newtitle = et.getText().toString().trim();
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("newtitle", newtitle);
                    editor.commit();
                }
            });
            builder.create().show();

            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     *                 ,               
     *
     * @return
     */
    private boolean isFirstEntry() {
        String password = sp.getString("password", "");
        return password.isEmpty();
    }

    private boolean isFirstSetup() {
        return (sp.getBoolean("lostset", false) == false);
    }

    /**
     *                   
     */
    private void showFirstEntryDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = View.inflate(this, R.layout.first_entry_dialog, null);
        et_first_dlg_pswd = (EditText) view.findViewById(R.id.et_first_dlg_pswd);
        et_first_dlg_pswd_confirm = (EditText) view.findViewById(R.id.et_first_dlg_pswd_confirm);
        bt_first_dlg_ok = (Button) view.findViewById(R.id.bt_first_dlg_ok);
        bt_first_dlg_cancel = (Button) view.findViewById(R.id.bt_first_dlg_cancel);

        //xml     android:onClick="onClick",       
        //bt_first_dlg_ok.setOnClickListener(this);
        //bt_first_dlg_cancel.setOnClickListener(this);

        builder.setView(view);
        dialog = builder.create();
        dialog.show();
    }

    /**
     *                    
     */
    private void showNormalEntryDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = View.inflate(this, R.layout.normal_entry_dialog, null);
        et_normal_dlg_pswd = (EditText) view.findViewById(R.id.et_normal_dlg_pswd);
        bt_normal_dlg_ok = (Button) view.findViewById(R.id.bt_normal_dlg_ok);
        bt_normal_dlg_cancel = (Button) view.findViewById(R.id.bt_normal_dlg_cancel);

        //xml     android:onClick="onClick",       
        //bt_normal_dlg_ok.setOnClickListener(this);
        //bt_normal_dlg_cancel.setOnClickListener(this);

        builder.setView(view);
        dialog = builder.create();
        dialog.show();
    }

    @Override
    public void onClick(android.view.View view) {
        switch (view.getId()) {
            case R.id.bt_first_dlg_cancel:
                dialog.cancel();
                finish();
                break;
            case R.id.bt_first_dlg_ok:
                String pswd = et_first_dlg_pswd.getText().toString().trim();
                String pswdconfirm = et_first_dlg_pswd_confirm.getText().toString().trim();

                //      
                if (pswd.isEmpty() || pswdconfirm.isEmpty()) {
                    Toast.makeText(this, "      ", 1).show();
                    return;
                }

                //            
                if (pswd.equals(pswdconfirm)) {
                    //    
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("password", util.md5String(pswd));
                    editor.commit();
                    dialog.dismiss();
                    //          ,        
                    finish();
                } else {
                    Toast.makeText(this, "       ", 1).show();
                    return;
                }
                break;
            case R.id.bt_normal_dlg_cancel:
                finish();
                break;
            case R.id.bt_normal_dlg_ok:
                String inputpswd = et_normal_dlg_pswd.getText().toString().trim();
                if (inputpswd.isEmpty()) {
                    Toast.makeText(this, "      ", 1).show();
                    return;
                }

                String savedpswd = sp.getString("password", "");
                if (util.md5String(inputpswd).equals(savedpswd)) {
                    Toast.makeText(this, "        ", 1).show();
                    dialog.dismiss();
                    return;
                } else {
                    Toast.makeText(this, "     ", 1).show();
                    return;
                }
        }
    }

}

インタフェースの効果: