InputCodeActivity.java 6.07 KB
package com.ectrip.cyt.ui;

import java.lang.reflect.Method;

import android.annotation.SuppressLint;
import android.content.Intent;
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 InputCodeActivity extends BaseActivity implements
		View.OnClickListener,LongClickRepeatListener {

	private String titleName;
	private EditText et_sfz;// 验证码
	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();
	}

	@SuppressLint("NewApi")
	private void initView() {
		SharedPreferences2Obj.getInstance(InputCodeActivity.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(getString(R.string.input_verify_code));
		}
		((TextView) findViewById(R.id.title)).setVisibility(View.VISIBLE);
		topBtns = (Button) findViewById(R.id.topBtns);
		topBtns.setVisibility(View.VISIBLE);

		findViewById(R.id.topBack).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		et_sfz = (EditText) findViewById(R.id.input_idcard);
		et_sfz.setHint(getString(R.string.input_verify_code));
		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(getString(R.string.topbtn_ok));
		topBtns.setOnClickListener(this);

		et_sfz.requestFocus();

		if (android.os.Build.VERSION.SDK_INT >= 11) {
			try {
				et_sfz.setShowSoftInputOnFocus(false);
			} catch (Exception e) {
				e.printStackTrace();
				textViewShow(et_sfz);
				// et_sfz.setInputType(InputType.TYPE_NULL);
			} catch (Throwable e) {
				e.printStackTrace();
				textViewShow(et_sfz);
				// et_sfz.setInputType(InputType.TYPE_NULL);
			}
		} else {
			textViewShow(et_sfz);
			// et_sfz.setInputType(InputType.TYPE_NULL);
		}
	}

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

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

				if (str == null || "".equals(str)) {
					MToast(InputCodeActivity.this,
							getString(R.string.verify_not_null),
							MToast.LENGTH_SHORT);
					return;
				}

				Intent intent = new Intent(InputCodeActivity.this,
						CodeOrderListActivity.class);
				intent.putExtra("mode", 0);
				intent.putExtra("code", 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;
		}
	}

	@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 (et_sfz.isFocused()) {
			index = et_sfz.getSelectionStart();
			editable = et_sfz.getText();
		}
		if (editable.length() > 0) {
			try {
				editable.delete(index - 1, index);
			} catch (Exception e) {
				if (editable.length() > 0) {
					editable.delete(index, index + 1);
				}
			}
		}
	}

}