效果如圖 1 首先這是一個自定義的Dialog,而不是AlertDialog,如果是AlertDialog的話,軟鍵盤彈出的時候在AlertDialog的後面,無法進行輸入。 2 Dialog的上面會有一個黑框,添加Style 3 輸入密碼用的是EditText,改變EditText的格式是 4 給 ...
效果如圖
1 首先這是一個自定義的Dialog,而不是AlertDialog,如果是AlertDialog的話,軟鍵盤彈出的時候在AlertDialog的後面,無法進行輸入。
2 Dialog的上面會有一個黑框,添加Style
<style name="dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <!-- 邊框 --> <item name="android:windowIsFloating">true</item> <!-- 是否浮現在activity之上 --> <item name="android:windowIsTranslucent">true</item> <!-- 半透明 --> <item name="android:windowNoTitle">true</item> <!-- 無標題 --> <item name="android:windowBackground">@color/transparent</item> <!-- 背景透明 --> <item name="android:backgroundDimEnabled">true</item> <!-- 模糊 --> </style>
3 輸入密碼用的是EditText,改變EditText的格式是
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/white" /> <corners android:radius="3dip"/> <stroke android:width="1dip" android:color="@color/viewfinder_mask" /> </shape>
4 給EditText添加監聽函數,本來想把EditText放入到List來管理,結果總是報錯指針錯誤,只能作罷- -,所以看起來有很多重覆的代碼啊,要監聽EditText的內容改變,限制輸入的內容,預設彈出數字鍵盤,監聽鍵盤的刪除鍵,以下是實現代碼
void initDialog() { current=0; myDialog = new Dialog(ChargeActivity.this, R.style.add_dialog); myDialog.show(); myDialog.getWindow().setContentView(R.layout.return_code_dialog); psw1 = (EditText) myDialog.getWindow().findViewById(R.id.psw_1); psw2 = (EditText) myDialog.getWindow().findViewById(R.id.psw_2); psw3 = (EditText) myDialog.getWindow().findViewById(R.id.psw_3); psw4 = (EditText) myDialog.getWindow().findViewById(R.id.psw_4); psw5 = (EditText) myDialog.getWindow().findViewById(R.id.psw_5); psw6 = (EditText) myDialog.getWindow().findViewById(R.id.psw_6); //鎖定數字鍵盤 psw1.setInputType(EditorInfo.TYPE_CLASS_NUMBER); psw2.setInputType(EditorInfo.TYPE_CLASS_NUMBER); psw3.setInputType(EditorInfo.TYPE_CLASS_NUMBER); psw4.setInputType(EditorInfo.TYPE_CLASS_NUMBER); psw5.setInputType(EditorInfo.TYPE_CLASS_NUMBER); psw6.setInputType(EditorInfo.TYPE_CLASS_NUMBER); psw1.addTextChangedListener(this); psw2.addTextChangedListener(this); psw3.addTextChangedListener(this); psw4.addTextChangedListener(this); psw5.addTextChangedListener(this); psw6.addTextChangedListener(this); psw1.setOnKeyListener(this); psw2.setOnKeyListener(this); psw3.setOnKeyListener(this); psw4.setOnKeyListener(this); psw5.setOnKeyListener(this); psw6.setOnKeyListener(this); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { int length =0; //Log.d("test","current="+current+"我變了"); switch(current){ case 0:length=psw1.getText().length();changeFocus(length,psw2);passwordInput[0]=psw1.getText().toString();break; case 1:length=psw2.getText().length();changeFocus(length,psw3);passwordInput[1]=psw2.getText().toString();break; case 2:length=psw3.getText().length();changeFocus(length,psw4);passwordInput[2]=psw3.getText().toString();break; case 3:length=psw4.getText().length();changeFocus(length,psw5);passwordInput[3]=psw4.getText().toString();break; case 4:length=psw5.getText().length();changeFocus(length,psw6);passwordInput[4]=psw5.getText().toString();break; case 5:length=psw6.getText().length();if(length>=1){passwordInput[5]=psw6.getText().toString();Log.d("input",passwordInput[5]);validatePassword();myDialog.cancel();} } } @Override public void afterTextChanged(Editable s) { } void changeFocus(int l,EditText editText){ if(l >= maxLen){ current++; //Log.d("test", "current=" + current+" 請求焦點"); editText.setFocusable(true); editText.setFocusableInTouchMode(true); editText.requestFocus(); editText.findFocus(); /**if(editText.isFocusable()){ Log.d("test",editText.getId()+"獲得焦點"); }*/ } } void validatePassword(){ current=0; boolean isInSchool =true; StringBuffer stringBuffer=new StringBuffer(); for(int i=0;i<passwordInput.length;i++){ stringBuffer.append(passwordInput[i]); } String psw =stringBuffer.toString(); boolean isPassword =psw.equals(password); Log.d("psw",passwordInput.toString()); Log.d("psw",password); if(!isPassword){ final Dialog errorDialog =new AlertDialog.Builder(ChargeActivity.this).create(); errorDialog.show(); errorDialog.getWindow().setContentView(R.layout.error_password_dialog); errorDialog.getWindow().findViewById(R.id.ok_error).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { errorDialog.cancel(); initDialog(); } }); }else if(!isInSchool){ final Dialog out =new AlertDialog.Builder(ChargeActivity.this).create(); out.show(); out.getWindow().setContentView(R.layout.out_school_dialog); out.getWindow().findViewById(R.id.ok_out_school).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { out.cancel(); } }); }else{ startActivity(new Intent(ChargeActivity.this,PaymentActivity.class)); } } @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) { current--; switch(current){ case 0:psw1.setText("");requestF(psw1);break; case 1:psw2.setText("");requestF(psw2);break; case 2:psw3.setText("");requestF(psw3);break; case 3:psw4.setText("");requestF(psw4);break; case 4:psw5.setText("");requestF(psw5);break; case 5:psw6.setText("");requestF(psw6);break; } } return false; } void requestF(EditText editText){ editText.setFocusable(true); editText.setFocusableInTouchMode(true); editText.requestFocus(); editText.findFocus(); }
寫的過程中犯了一個比較傻的錯誤,字元串數組沒分配記憶體,結果一直報指針錯誤,傻呵呵的調了半天= =
還有就是一些邏輯細節的問題,向刪除的時候比較容易引起焦點混亂。