InputIdCardActivity.java 9.75 KB
package com.ectrip.cyt.ui;

import java.lang.reflect.Method;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ectrip.cyt.shield_home.LockLayer.MToast;
import com.ectrip.cyt.utils.ActivitiesManager;
import com.ectrip.cyt.utils.CommetryUtils;
import com.ectrip.cyt.utils.CountDownTimer;
import com.ectrip.cyt.utils.LogUtil;
import com.ectrip.cyt.utils.RegexUtils;
import com.ectrip.cyt.utils.SharedPreferences2Obj;
import com.ectrip.trips.check.R;
import com.ectrip.trips.view.LongClickButton;
import com.ectrip.trips.view.LongClickButton.LongClickRepeatListener;

/**
 * @author Administrator 身份证输入页面
 */
public class InputIdCardActivity extends BaseActivity implements
        View.OnClickListener, LongClickRepeatListener {

    private String titleName;
    // 身份证
    private EditText idEdit;
    private TextView one, two, three, four, five, six, seven, eight, nine, zero,
            X_btn, topBtns;
    private LongClickButton delete;
    private int tag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_input_idcard);
        ActivitiesManager.getInstance().pushActivity(this);
        initConfig();
        initView();
        initReceiver();
    }

    private void initReceiver() {
        IntentFilter filter = new IntentFilter("cyt.idcard.action");
        registerReceiver(broadcastReceiver, filter);
    }

    private void initConfig() {
        tag = getIntent().getIntExtra("tag", -1);
        titleName = getIntent().getStringExtra("titleName");
        if (tag == 0) {
            ((TextView) findViewById(R.id.title))
                    .setText(titleName == null ? getString(R.string.app_name)
                            : getString(R.string.order_id) + titleName);

        } else {
            ((TextView) findViewById(R.id.title))
                    .setText(titleName == null ? getString(R.string.app_name)
                            : titleName);
        }
        findViewById(R.id.topBack).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
    }

    @SuppressLint("NewApi")
    private void initView() {
        SharedPreferences2Obj.getInstance(InputIdCardActivity.this)
                .setName("SelectAction").setObject("isStatistic", "0"); // 非统计
        titleName = getIntent().getStringExtra("titleName");
        if (titleName != null) {
            ((TextView) findViewById(R.id.title)).setText(titleName);
        } else {
            ((TextView) findViewById(R.id.title)).setText(R.string.input_id);
        }
        ((TextView) findViewById(R.id.title)).setVisibility(View.VISIBLE);
        topBtns = (Button) findViewById(R.id.topBtns);
        topBtns.setVisibility(View.VISIBLE);

        idEdit = (EditText) findViewById(R.id.input_idcard);
        one = (TextView) findViewById(R.id.one);
        one.setOnClickListener(this);

        two = (TextView) findViewById(R.id.two);
        two.setOnClickListener(this);

        three = (TextView) findViewById(R.id.three);
        three.setOnClickListener(this);

        four = (TextView) findViewById(R.id.four);
        four.setOnClickListener(this);

        five = (TextView) findViewById(R.id.five);
        five.setOnClickListener(this);

        six = (TextView) findViewById(R.id.six);
        six.setOnClickListener(this);

        seven = (TextView) findViewById(R.id.seven);
        seven.setOnClickListener(this);

        eight = (TextView) findViewById(R.id.eight);
        eight.setOnClickListener(this);

        nine = (TextView) findViewById(R.id.nine);
        nine.setOnClickListener(this);

        zero = (TextView) findViewById(R.id.zero);
        zero.setOnClickListener(this);

        X_btn = (TextView) findViewById(R.id.X_btn);
        X_btn.setOnClickListener(this);

        delete = (LongClickButton) findViewById(R.id.delete);
        delete.setLongClickRepeatListener(this);
        delete.setOnClickListener(this);

        topBtns.setText(R.string.topbtn_ok);
        topBtns.setOnClickListener(this);

        if (tag == 0) {
            idEdit.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
            idEdit.setTransformationMethod(PasswordTransformationMethod
                    .getInstance());
            idEdit.setHint(R.string.ticket_counter_pwd);
        }

        if (android.os.Build.VERSION.SDK_INT >= 28) {
            try {
                idEdit.setShowSoftInputOnFocus(false);
                idEdit.requestFocus();
            } catch (Exception e) {
                e.printStackTrace();
                textViewShow(idEdit);
            } catch (Throwable e) {
                e.printStackTrace();
                textViewShow(idEdit);
            }
        } else if (android.os.Build.VERSION.SDK_INT >= 11) {
            try {
                idEdit.setShowSoftInputOnFocus(false);
            } catch (Exception e) {
                e.printStackTrace();
                textViewShow(idEdit);
            } catch (Throwable e) {
                e.printStackTrace();
                textViewShow(idEdit);
            }
        } else {
            textViewShow(idEdit);
        }
    }

    private void textViewShow(EditText message) {

        if (android.os.Build.VERSION.SDK_INT <= 10) {
            message.setInputType(InputType.TYPE_NULL);
        } else {
            getWindow().setSoftInputMode(
                    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
            try {
                Class<EditText> cls = EditText.class;
                Method setSoftInputShownOnFocus = cls.getMethod(
                        "setSoftInputShownOnFocus", boolean.class);
                setSoftInputShownOnFocus.setAccessible(true);
                setSoftInputShownOnFocus.invoke(message, false);
            } catch (Exception e) {
            }
        }

    }

    @Override
    public void onClick(View v) {
        int index = 0;
        Editable editable = null;
        if (idEdit.isFocused()) {
            LogUtil.d(TAG, "获取到了焦点");
            index = idEdit.getSelectionStart();
            editable = idEdit.getText();
        }

        switch (v.getId()) {
            case R.id.topBtns:
                String str = idEdit.getText().toString();

                if (!RegexUtils.checkIdCard(str)) {
                    MToast(InputIdCardActivity.this,
                            getString(R.string.the_idcard_not_valid),
                            MToast.LENGTH_SHORT);
                    idEdit.setText("");
                    return;
                }
                Intent intent = new Intent(InputIdCardActivity.this,
                        IDOrderListActivity.class);
                CountDownTimer.firstTime = new Date().getTime();// 不必要的可以删除
                intent.putExtra("mode", 0);
                intent.putExtra("idcardNumber", str);
                intent.putExtra("titleName", getString(R.string.order_list));
                startActivity(intent);

                break;
            case R.id.delete:
                if (idEdit.isFocused()) {
                    index = idEdit.getSelectionStart();
                    editable = idEdit.getText();
                }
                if (editable.length() > 0) {
                    try {
                        editable.delete(index - 1, index);
                    } catch (Exception e) {
                        if (editable.length() > 0) {
                            editable.delete(index, index + 1);
                        }
                    }
                }
                break;
            default:
                if (editable.length() < 18) {
                    editable.insert(index, ((TextView) v).getText());
                }
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            try {
                CommetryUtils.releaseCommery(this);
            } catch (Exception e) {
                e.printStackTrace();
            }

            finish();

        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        try {
            CommetryUtils.releaseCommery(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void repeatAction() {
        int index = 0;
        Editable editable = null;
        if (idEdit.isFocused()) {
            index = idEdit.getSelectionStart();
            editable = idEdit.getText();
        }
        if (editable.length() > 0) {
            try {
                editable.delete(index - 1, index);
            } catch (Exception e) {
                if (editable.length() > 0) {
                    editable.delete(index, index + 1);
                }
            }
        }
    }

    /**************************查询成功的情况下,清空输入框*****************************/
    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            boolean sucess = intent.getExtras().getBoolean("sucess", false);
            if (sucess) {
                idEdit.setText("");
            }
        }
    };
}