android 驗證二

來源:互聯網
上載者:User

標籤:android   style   blog   http   ar   io   color   os   sp   

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="fill_parent"      android:layout_height="match_parent"      android:orientation="vertical"      tools:context=".MainActivity" >             <TextView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:id="@+id/name"            android:text="姓名:"/>      <EditText android:id="@+id/editText_name"           android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:hint="請輸入文本資訊"          />            <TextView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:id="@+id/age"            android:text="年齡:"/>             <EditText android:id="@+id/editText_age"           android:layout_height="wrap_content"           android:layout_width="fill_parent"           android:hint="請輸入年齡"/>        <Button android:id="@+id/btnSubmit"          android:layout_height="wrap_content"          android:layout_width="wrap_content"          android:text="提交驗證"/>  </LinearLayout>  
package com.example.fourandroid;    import android.annotation.SuppressLint;  import android.app.Activity;  import android.app.AlertDialog;  import android.content.DialogInterface;  import android.os.Bundle;  import android.view.View;  import android.view.View.OnClickListener;  import android.widget.Button;  import android.widget.EditText;  import android.widget.Toast;    @SuppressLint("ShowToast")  public class MainActivity extends Activity {      private EditText editText_name;      private EditText editText_age;      private Button btnSubmit;            @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          super.setContentView(R.layout.activity_main);                    //擷取組件資訊;          this.editText_name = (EditText) super.findViewById(R.id.editText_name);          this.editText_age = (EditText)super.findViewById(R.id.editText_age);                    //給按鈕設定單擊事件;          this.btnSubmit = (Button)super.findViewById(R.id.btnSubmit);          this.btnSubmit.setOnClickListener(new myBtnSubmitImpl());      }        public class myBtnSubmitImpl implements OnClickListener{          @Override          public void onClick(View v) {              boolean bol = true;                            String age = MainActivity.this.editText_age.getText().toString();              String name = MainActivity.this.editText_name.getText().toString();              //執行個體化對話方塊;              AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());              builder.setTitle("提示資訊");                            //判斷name是否為空白;              if(name == null || name.trim().length() == 0 || "".equals(name.trim())){                  builder.setMessage("Name不可為空,請輸入資訊.");                  builder.setPositiveButton("確認", new DialogInterface.OnClickListener(){                      @Override                      public void onClick(DialogInterface dialog, int which) {                          setResult(RESULT_OK);                       }                  });                                    //設定焦點資訊;                  MainActivity.this.editText_name.setFocusable(true);                  MainActivity.this.editText_name.setFocusableInTouchMode(true);                  MainActivity.this.editText_name.requestFocus();                  MainActivity.this.editText_name.requestFocusFromTouch();                  builder.show();                  //標識此文字框屬性為空白;                  bol = false;                  return;              }                            //判斷address是否為空白;              if(age == null || "".equals(age) || age.trim().length() == 0){                  builder.setMessage("Age不可為空,請輸入資訊.");                  //給對話方塊增加一個確定按鈕;                  builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {                       //監聽單擊事件;                      public void onClick(DialogInterface dialog, int whichButton) {                           setResult(RESULT_OK);//確定按鈕事件 ;                      }                   }) ;                                    //設定address焦點;                  MainActivity.this.editText_age.setFocusable(true);                  MainActivity.this.editText_age.setFocusableInTouchMode(true);                  MainActivity.this.editText_age.requestFocus();                  MainActivity.this.editText_age.requestFocusFromTouch();                                    //顯示提示框;                  builder.show();                  //標識此文字框屬性為空白;                  bol = false;                  return;              }                            if(bol){                  //設定提示資訊.                  Toast.makeText(MainActivity.this, "恭喜你,驗證成功", Toast.LENGTH_LONG).show();              }          }      }  }  

 

android 驗證二

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.