短信验证

来源:互联网 发布:windows强制关机快捷键 编辑:程序博客网 时间:2024/06/05 04:27

在清单文件中添加控件例如:

<RelativeLayout    android:layout_width="match_parent"    android:layout_height="wrap_content">    <TextView        android:id="@+id/tv_x"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:onClick="exit"        android:padding="10dp"        android:text="X"        android:textSize="20sp" /></RelativeLayout><RelativeLayout    android:layout_width="match_parent"    android:layout_height="wrap_content">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:text="登录你的头条,精彩永不丢失"        android:textColor="#2A2A2A"        android:textSize="18dp" /></RelativeLayout><RelativeLayout    android:layout_width="match_parent"    android:layout_height="wrap_content">    <EditText        android:id="@+id/editText"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="20dp"        android:layout_marginRight="20dp"        android:layout_marginTop="20dp"        android:background="@drawable/background"        android:hint="请输入手机号"        android:padding="15dp" />    <TextView        android:id="@+id/tv_sendMessage"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/editText"        android:layout_alignBottom="@+id/editText"        android:layout_alignParentEnd="true"        android:layout_alignParentRight="true"        android:layout_marginEnd="29dp"        android:layout_marginRight="29dp"        android:text="|  发送验证码"        android:textSize="18sp" /></RelativeLayout><RelativeLayout    android:layout_width="match_parent"    android:layout_height="wrap_content">    <EditText        android:id="@+id/et_yanzhengma"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="20dp"        android:layout_marginRight="20dp"        android:layout_marginTop="15dp"        android:background="@drawable/background"        android:hint="请输入验证码"        android:padding="15dp" /></RelativeLayout><RelativeLayout    android:layout_width="match_parent"    android:layout_height="wrap_content">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="未注册手机验证后自动登录" /></RelativeLayout><Button    android:id="@+id/btn_jinrutoutiao"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginLeft="20dp"    android:layout_marginRight="20dp"    android:layout_marginTop="10dp"    android:background="@drawable/background_fen"    android:text="进入头条"    android:textColor="#FEF4F4"    android:textSize="18sp" /><RelativeLayout    android:layout_width="match_parent"    android:layout_height="wrap_content">    <CheckBox        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:text="我已阅读并同意'用户协议和隐私条款'" /></RelativeLayout><RelativeLayout    android:layout_width="match_parent"    android:layout_height="wrap_content">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:gravity="center"        android:text="账号密码登录"        android:textColor="#5777A5"        android:textSize="16dp" /></RelativeLayout><RelativeLayout    android:layout_width="match_parent"    android:layout_height="wrap_content">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="100dp"        android:gravity="center"        android:text="-------更多登录方式------"        android:textSize="16sp" /></RelativeLayout><LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginTop="10dp"    android:gravity="center"    android:orientation="horizontal">    <ImageView        android:layout_width="50sp"        android:layout_height="50sp"        android:layout_weight="1"        android:src="@drawable/weixin" />    <ImageView        android:id="@+id/imageView_qq"        android:layout_width="60sp"        android:layout_height="60sp"        android:layout_weight="1"        android:padding="5dp"        android:src="@drawable/qq" />    <ImageView        android:layout_width="50sp"        android:layout_height="50sp"        android:layout_weight="1"        android:padding="5dp"        android:src="@drawable/weibo" />    <ImageView        android:layout_width="50sp"        android:layout_height="50sp"        android:layout_weight="1"        android:src="@drawable/youxiang" />    <ImageView        android:layout_width="50sp"        android:layout_height="50sp"        android:layout_weight="1"        android:src="@drawable/tianyi" /></LinearLayout>在mainActivity中写的代码
private EditText yanzhengma;private EditText phoneNumber;private TextView tv_sendMessage;private Button btn_jinrutoutiao;private int TIME = 5;private final int SECOND = 1000;private EventHandler eventHandler;Handler timeHandler = new Handler();Runnable timeRunable = new Runnable() {    @Override    public void run() {        TIME--;        if (TIME == 0) {            timeHandler.removeCallbacks(this);            TIME = 5;            tv_sendMessage.setEnabled(true);            tv_sendMessage.setText("再次获取");        } else {            tv_sendMessage.setEnabled(false);            tv_sendMessage.setTextColor(getResources().getColor(R.color.colorPrimaryDark));            tv_sendMessage.setText(TIME + "s");            timeHandler.postDelayed(this, SECOND);        }    }};private ImageView qq;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_message_login);//这个是页面的布局    TextView tv_x = (TextView) findViewById(R.id.tv_x);    yanzhengma = (EditText) findViewById(R.id.et_yanzhengma);    phoneNumber = (EditText) findViewById(R.id.editText);    tv_sendMessage = (TextView) findViewById(R.id.tv_sendMessage);    btn_jinrutoutiao = (Button) findViewById(R.id.btn_jinrutoutiao);    qq = (ImageView) findViewById(R.id.imageView_qq);    qq.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View view) {            Intent intent = new Intent(MessageLoginActivity.this,QQloginActivity.class);            startActivity(intent);        }    });    tv_sendMessage.setOnClickListener(this);    btn_jinrutoutiao.setOnClickListener(this);    registerSMS();    tv_x.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View view) {            finish();        }    });}private void registerSMS() {        // 创建EventHandler对象        eventHandler = new EventHandler() {            public void afterEvent(int event, int result, Object data) {                if (data instanceof Throwable) {                    Throwable throwable = (Throwable) data;                    final String msg = throwable.getMessage();                    runOnUiThread(new Runnable() {                        @Override                        public void run() {                            Toast.makeText(MessageLoginActivity.this, msg, Toast.LENGTH_SHORT).show();                        }                    });                } else {                    if (result == SMSSDK.RESULT_COMPLETE) {//只有回服务器验证成功,才能允许用户登录                        //回调完成,提交验证码成功                        if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {                            runOnUiThread(new Runnable() {                                @Override                                public void run() {                                    Toast.makeText(MessageLoginActivity.this, "服务器验证成功", Toast.LENGTH_SHORT).show();                                    User user = new User();                                    user.uid = phoneNumber.getText().toString();                                    user.phone = phoneNumber.getText().toString();                                    SharedPreferencesUtil.putPreferences("uid", user.uid);                                    SharedPreferencesUtil.putPreferences("phone", user.phone);                                }                            });                        }                    } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){                        //获取验证码成功                        runOnUiThread(new Runnable() {                            @Override                            public void run() {                                Toast.makeText(MessageLoginActivity.this,"验证码已发送",Toast.LENGTH_SHORT).show();                            }                        });                    } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {                        //返回支持发送验证码的国家列表                    }                }            }        };        // 注册监听器        SMSSDK.registerEventHandler(eventHandler);}//执行的一些判断@Overridepublic void onClick(View view) {    switch (view.getId()){        case R.id.btn_jinrutoutiao:            verify();            SMSSDK.submitVerificationCode("86", phoneNumber.getText().toString(), yanzhengma.getText().toString());            break;        case R.id.tv_sendMessage:            if (TextUtils.isEmpty(phoneNumber.getText().toString())) {                Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();                return;            }            timeHandler.postDelayed(timeRunable, SECOND);            SMSSDK.getVerificationCode("86", phoneNumber.getText().toString());            break;    }}private void verify() {    if (TextUtils.isEmpty(phoneNumber.getText().toString())) {        Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();        return;    }    if (TextUtils.isEmpty(yanzhengma.getText().toString())) {        Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();        return;    }}


原创粉丝点击