參考連結:http://pulse7.net/android/login-dialog-box-android/
MainActivity.java
package com.example.mycs.alertdialoglogin; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.method.PasswordTransformationMethod; import android.view.LayoutInflater; import android.view.View; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { public void btnClicked (View view) { LayoutInflater inflater = getLayoutInflater(); @SuppressLint("InflateParams") View alertLayout = inflater.inflate(R.layout.login_dialog, null); final EditText etUsername = alertLayout.findViewById(R.id.textUsername); final EditText etPassword = alertLayout.findViewById(R.id.textPassword); final CheckBox cbToggle = alertLayout.findViewById(R.id.cb_show_pass); cbToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { if (isChecked) { etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance()); } else { etPassword.setTransformationMethod(null); } } }); AlertDialog.Builder login = new AlertDialog.Builder(this); login.setTitle("Login"); login.setView(alertLayout); login.setCancelable(false); login.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(getBaseContext(), "Cancel clicked.", Toast.LENGTH_SHORT).show(); } }); login.setPositiveButton("Login", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { String user = etUsername.getText().toString(); String pass = etPassword.getText().toString(); Toast.makeText(getBaseContext(), "User: " + user + " Password: " + pass + " Logged in.", Toast.LENGTH_LONG).show(); } }); AlertDialog dialog = login.create(); dialog.show(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
心得:學到如何實作顯示密碼欄位內容,覺得有用。
2.
參考連結︰https://www.android-examples.com/show-create-alertdialog-with-multiple-choices-in-android/
MainActivity.java
package com.example.mycs.multichoicedialog; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.Arrays; import java.util.List; public class MainActivity extends AppCompatActivity { Button button; TextView textView; AlertDialog.Builder alertDialogBuilder; String[] alertDialogItems = new String[] { "美味蟹堡", "義式香腸堡", "蔬菜水果堡", "香蕉潛艇堡", "香烤雞腿堡" }; List itemsIntoList; boolean[] selected = new boolean[] { false, false, false, false, false }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.button); textView = findViewById(R.id.textView); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { textView.setText(""); alertDialogBuilder = new AlertDialog.Builder(MainActivity.this); itemsIntoList = Arrays.asList(alertDialogItems); alertDialogBuilder.setMultiChoiceItems(alertDialogItems, selected, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i, boolean b) { } }); alertDialogBuilder.setCancelable(false); alertDialogBuilder.setTitle("您點的餐點有:"); alertDialogBuilder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { int index = 0; while (index < selected.length) { boolean value = selected[index]; if (value) { textView.setText(String.format("%s%s\n", textView.getText(), itemsIntoList.get(index))); } index++; } } }); alertDialogBuilder.setNeutralButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }); AlertDialog diaLog = alertDialogBuilder.create(); diaLog.show(); } }); } }
心得:蠻直覺的,沒用到DialogFragment之類的東東,簡明易懂,基本上直接讀code就知道每一行程式碼是在做些什麼事情,應該可以輕易地改成Button的onClick實做。
3.
參考連結:https://youtu.be/33BFCdL0Di0
MainActivity.java
心得:要呼叫DatePickerDialog得要先傳值給它,不然他就是不開,然後API也要24以上,真任性!package com.example.mycs.datepickdialog; import android.annotation.TargetApi; import android.app.DatePickerDialog; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; import java.text.DateFormat; import java.util.Calendar; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button button; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.button); textView = findViewById(R.id.textView); button.setOnClickListener(this); } @TargetApi(Build.VERSION_CODES.N) @RequiresApi(api = Build.VERSION_CODES.N) @Override public void onClick(View view) { final Calendar calendar = Calendar.getInstance(); int mYear = calendar.get(Calendar.YEAR); int mMonth = calendar.get(Calendar.MONTH); int mDay = calendar.get(Calendar.DAY_OF_MONTH); DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month); cal.set(Calendar.DAY_OF_MONTH, dayOfMonth); String currentDateString = DateFormat.getDateInstance(DateFormat.FULL).format(cal.getTime()); textView.setText(currentDateString); } }, mYear, mMonth, mDay); datePickerDialog.show(); } }
4. 搭配ProgressBar的ProgressDialog
參考連結:https://www.journaldev.com/9652/android-progressdialog-example
MainActivity.java
package com.example.mycs.progressdialog; import android.annotation.SuppressLint; import android.app.ProgressDialog; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button button; ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setMax(100); progressDialog.setMessage("It's loading..."); progressDialog.setTitle("Progress Dialog with bar example"); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.show(); new Thread(new Runnable() { @Override public void run() { try { while (progressDialog.getProgress() <= progressDialog.getMax()) { Thread.sleep(200); handle.sendMessage(handle.obtainMessage()); if(progressDialog.getProgress() == progressDialog.getMax()) { progressDialog.dismiss(); } } } catch (Exception e) { e.printStackTrace(); } } }).start(); } @SuppressLint("HandlerLeak") Handler handle = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); progressDialog.incrementProgressBy(1); } }; }); } }
心得:因為ProgressDialog本身要在原地等候ProgressBar跑完不能動,所以調用一個新的執行緒(Thread),來handle進度。
No comments:
Post a Comment