Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> 提示框的優化之自定義Toast組件之(三)Toast組件優化,toast組件

提示框的優化之自定義Toast組件之(三)Toast組件優化,toast組件

編輯:關於android開發

提示框的優化之自定義Toast組件之(三)Toast組件優化,toast組件


開發步驟:

  • 在toast_customer.xml文件中添加一個圖片組件對象顯示提示圖片
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout ......>
 3 
 4     <ImageView android:id="@+id/toastIcon"
 5         android:layout_width="24dp"
 6         android:layout_height="24dp"
 7         android:layout_gravity="center_vertical"/>
 8 
 9 ......
10 
11 </LinearLayout>
  • 在LoginActivity.java類中的自定義Toast調用方法中添加對圖片組件的設置
 1 public class LoginActivity extends AppCompatActivity {
 2 
 3     ........
 4 
 5     private void showCustomerToast(final int icon, final String message){
 6         LayoutInflater inflater=getLayoutInflater();
 7         View layout=inflater.inflate(R.layout.toast_customer, (ViewGroup) findViewById(R.id.toast_layout_root));
 8         ......
 9 
10         //圖片組件的設置
11         ImageView toastIcon=(ImageView)layout.findViewById(R.id.toastIcon);  
12         toastIcon.setBackgroundResource(icon);   
13 
14         ......
15 
16         Toast toast=new Toast(getApplicationContext());
17         toast.setDuration(Toast.LENGTH_LONG);
18         toast.setView(layout);
19         toast.show();
20     }
21    ......
22 }
  • 調用該方法
 1 public class LoginActivity extends AppCompatActivity {
 2     ......
 3     private  class ViewOcl implements View.OnClickListener{
 4        public void onClick (View v){
 5          ......
 6          if (login_flag) {
 7               showCustomerToast(android.R.drawable.ic_menu_call,"歡迎登錄," + account);
 8               ...... 
 9          }
10          else {
11               showCustomerToast(android.R.drawable.ic_delete,"賬號或密碼錯誤");
12          }
13          break;
14          ......
15        }
16     }
17     ......
18 }

運行:

1 public class Register_Activity extends AppCompatActivity { 2 ...... 3 private boolean checkform() { 4 if (this.txtRegAccount.getText().toString() == null || this.txtRegAccount.getText().toString().length() == 0) { 5 6 //Toast.makeText(getApplicationContext(), "警告:注冊賬號不能為空", Toast.LENGTH_LONG).show(); 7 showCustomerToast(android.R.drawable.ic_delete, "警告:注冊賬號不能為空"); 8 return false; 9 } 10 if (this.txtRegPassword.getText().toString() == null || this.txtRegPassword.getText().toString().length() == 0) { 11 //Toast.makeText(getApplicationContext(), "警告:注冊密碼不能為空", Toast.LENGTH_LONG).show(); 12 showCustomerToast(android.R.drawable.ic_delete,"警告:注冊密碼不能為空"); 13 return false; 14 } 15 if (!(this.txtRegPassword.getText().toString()).equals((this.txtReRegPassword.getText().toString()))) { 16 //Toast.makeText(getApplicationContext(), "警告:兩次密碼不一致", Toast.LENGTH_LONG).show(); 17 showCustomerToast(android.R.drawable.ic_delete,"警告:兩次密碼不一致"); 18 return false; 19 } 20 return true; 21 } 22 ...... 23 }

運行:

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved