歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android第一個UI界面學習:登錄到對話框

Android第一個UI界面學習:登錄到對話框

日期:2017/3/1 11:08:45   编辑:Linux編程

一開始布局就再login_dlg.xml文件中拖了兩個文本框,兩個編輯框,兩個按鈕,此處學校到對於布局是屬性中如果前面有layout的就是表示該控件和父控件到關系,如果沒有到就表示該控件到子控件和該控件到關系。後來發現按鈕可以直接使用對話框上到就行了

然後就開始著手把它放到對話框上。說實話,以前沒使用過JAVA,怎麼創建對話框都不知道,真夠嗆的。

其實我也並不是沒有頭緒,因為我知道,再Android到SDK自帶了很多源碼,不會再裡面看看就知道了。再SDK中到ApiDemos工程下面,各種控件到效果,實現代碼都是有到。我到登錄對話框代碼就是再哪裡找到的,你說難不難。

下面是從API Demos下面拷貝過來一段代碼放到我到工程中,主要就是對對話框到onCreateDialog進行重寫。

  1. public class HelloWorld extends Activity
  2. {
  3. /** Called when the activity is first created. */
  4. @Override
  5. public void onCreate(Bundle savedInstanceState)
  6. {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.main);
  9. showDialog(1);
  10. }
  11. @Override
  12. protected Dialog onCreateDialog(int id) {
  13. LayoutInflater factory = LayoutInflater.from(this);
  14. final View textEntryView = factory.inflate(R.layout.login_dlg, null);
  15. return new AlertDialog.Builder(HelloWorld.this)
  16. .setIcon(R.drawable.icon)
  17. .setTitle(R.string.land_dialog_title)
  18. .setView(textEntryView)
  19. .setPositiveButton(R.string.land_dialog_ok, new DialogInterface.OnClickListener() {
  20. public void onClick(DialogInterface dialog, int whichButton) {
  21. //處理登錄
  22. Toast.makeText(HelloWorld.this,getText(R.string.land_select_ok), Toast.LENGTH_SHORT).show();
  23. }
  24. })
  25. .setNegativeButton(R.string.land_dialog_cancel, new DialogInterface.OnClickListener() {
  26. public void onClick(DialogInterface dialog, int whichButton) {
  27. //處理取消登錄
  28. Toast.makeText(HelloWorld.this,getText(R.string.land_select_cancel), Toast.LENGTH_SHORT).show();
  29. }
  30. })
  31. .create();
  32. }
  33. }

下面是我的login_dlg.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:id="@+id/username"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_marginLeft="20dip"
  12. android:layout_marginRight="20dip"
  13. android:textColor="#ff0000"
  14. android:gravity="left"
  15. android:text="用戶名:"
  16. />
  17. <EditText
  18. android:id="@+id/txt_username"
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:layout_marginLeft="20dip"
  22. android:layout_marginRight="20dip"
  23. android:gravity="fill_horizontal"
  24. android:autoText="false"
  25. android:capitalize="none"
  26. android:text="請輸入用戶名"
  27. />
  28. <TextView
  29. android:id="@+id/password"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_marginLeft="20dip"
  33. android:layout_marginRight="20dip"
  34. android:textColor="#ff0000"
  35. android:gravity="left"
  36. android:text="密碼:"
  37. />
  38. <EditText
  39. android:id="@+id/txt_password"
  40. android:layout_width="fill_parent"
  41. android:layout_height="wrap_content"
  42. android:layout_marginLeft="20dip"
  43. android:layout_marginRight="20dip"
  44. android:gravity="fill_horizontal"
  45. android:autoText="false"
  46. android:password="true"
  47. android:capitalize="none"
  48. android:text="請輸入密碼"
  49. />
  50. </LinearLayout>
Copyright © Linux教程網 All Rights Reserved