InputPhoneActivity.java 9.04 KB
package com.ectrip.cyt.ui;

import java.lang.reflect.Method;
import java.util.regex.Pattern;

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.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.SharedPreferences2Obj;
import com.ectrip.trips.check.R;
import com.ectrip.trips.view.LongClickButton;
import com.ectrip.trips.view.LongClickButton.LongClickRepeatListener;

/**
 * 手机号输入界面
 */
public class InputPhoneActivity extends BaseActivity implements
        View.OnClickListener, LongClickRepeatListener {

    private String titleName;
    private EditText phoneEdit;// 手机号
    private TextView one, two, three, four, five, six, seven, eight, nine,
            zero, X_btn, topBtns;
    private LongClickButton delete;

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

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

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


//	unregisterReceiver(broadcastReceiver);
    }

    @SuppressLint("NewApi")
    private void initView() {
        SharedPreferences2Obj.getInstance(InputPhoneActivity.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_phone);
        }
        ((TextView) findViewById(R.id.title)).setVisibility(View.VISIBLE);
        findViewById(R.id.topBack).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        topBtns = (Button) findViewById(R.id.topBtns);
        topBtns.setVisibility(View.VISIBLE);

        phoneEdit = (EditText) findViewById(R.id.input_idcard);
        phoneEdit.setHint(R.string.input_phone);
        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.setOnClickListener(this);
        delete.setLongClickRepeatListener(this);
        topBtns.setText(R.string.topbtn_ok);
        topBtns.setOnClickListener(this);

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

    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 (phoneEdit.isFocused()) {
            index = phoneEdit.getSelectionStart();
            editable = phoneEdit.getText();
        }

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

                if (str == null) {
                    MToast(InputPhoneActivity.this,
                            getString(R.string.fill_in_phone), MToast.LENGTH_SHORT);
                    return;
                }

                if (!isNumeric(str)) {
                    MToast(InputPhoneActivity.this,
                            getString(R.string.phone_incorrect),
                            MToast.LENGTH_SHORT);
                    return;
                }

                Intent intent = new Intent(InputPhoneActivity.this,
                        PhoneOrderListActivity.class);
                intent.putExtra("mode", 0);
                intent.putExtra("mobile", str);
                intent.putExtra("titleName", getString(R.string.order_list));
                startActivity(intent);
                break;
            case R.id.delete:
                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;
        }
    }

    public boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        if (pattern.matcher(str).matches() && str.length() == 11) {
            return true;
        } else {
            return false;
        }
    }

    @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();
        unregisterReceiver(broadcastReceiver);
        try {
            CommetryUtils.releaseCommery(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void repeatAction() {
        int index = 0;
        Editable editable = null;
        if (phoneEdit.isFocused()) {
            index = phoneEdit.getSelectionStart();
            editable = phoneEdit.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) {
                phoneEdit.setText("");
            }
        }
    };
}