در مرحله بعد از آن، دو دکمه را به کاغذ ورود خویش اضافه میکنیم: « Cancel » و « Next ». ما از مولفه MDC Button به کارگیری طراحی اپلیکیشن در مشهد خواهیم کرد که با یک استایل موج دار جوهری Material Design دکمه ها را اضافه می نماید . در shr_login_fragment.xml، یک را به ، در پایین موادسازنده TextInputLayout اضافه نمایید. آن گاه دو عنصر را به اضافه فرمائید.
پوشه XML نتایج بایستی به صورت ذیل باشد:
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="@string/shr_button_next" />
android:id="@+id/cancel_button"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
android:layout_toStartOf="@id/next_button"
android:layout_toLeftOf="@id/next_button"
android:text="@string/shr_button_cancel" />
COPY
اکنون میبینید وقتی که نرمافزار را جاری ساختن میکنید، با ضربه زدن روی هر دکمه، یک موج جوهر نشان داده می شود.
رفتن به بخش آجل
در غایت، کد جاوا را به LoginFragment.java اضافه میکنیم تا دکمه “NEXT” خویش را به قطعه دیگری متصل کنیم.
متوجه خواهید شد که هریک از مؤلفه هایی که به طرح خویش اضافه کردیم، یک شناسه به آن تخصیص داده شدهاست.
ما از این شناسهها برای ارجاع به مؤلفههای کدمان استعمال می کنیم و بعضا از قابلیت و امکان ها مانند پژوهش مشکلات و پیمایش را اضافه میکنیم.
بیایید یک مشی بولی محرمانه isPasswordValid در LoginFragment.java در ذیل () onCreateView اضافه کنیم که منطقی برای گزینش اینکه آیا راز عبور دارای اعتبار میباشد یا این که خیرمی باشد.
برای هدف ها این دمو، ما تنها مطمئن می شویم که راز عبور دستکم 8 کاراکتر باشد:
/*
In reality, this will have more complex logic including, but not limited to, actual
authentication of the username and password.
*/
private boolean isPasswordValid(@Nullable Editable text) {
return text != null && text.length() >= 8;
}
COPY
در مرحله سپس، یک کلیک listener به دکمه « NEXT» اضافه فرمائید که اشتباه را مبنی بر مشی () isPasswordValid که ساخت کردیم تهیه و منزه می نماید.
در () onCreateView، این کلیک listener می بایست در بین خط inflater و خط نمای بازگشتی قرار گیرد.
در مرحله بعد از آن، بیایید یک listener کلیدی به راز عبور TextInputEditText اضافه کنیم تا به رویدادهای کلیدی که غلط را تمیز می نمایند دقت داشته باشیم.
این listener همینطور می بایست از () isPasswordValid برای نظارت دارای اعتبار بودن یا این که نبودن سر عبور به کار گیری نماید.
میتوانید این گزینه را مستقیماً ذیل کلیک مستمع در () onCreateView اضافه نمائید.
سیاق onCreateView شما فعلا می بایست چیزی مشابه بهاین باشد:
@Override
public View onCreateView(
@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.shr_login_fragment, container, false);
final TextInputLayout passwordTextInput = view.findViewById(R.id.password_text_input);
final TextInputEditText passwordEditText = view.findViewById(R.id.password_edit_text);
MaterialButton nextButton = view.findViewById(R.id.next_button);
// Set an error if the password is less than 8 characters.
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!isPasswordValid(passwordEditText.getText())) {
passwordTextInput.setError(getString(R.string.shr_error_password));
} else {
passwordTextInput.setError(null); // Clear the error
}
}
});
// Clear the error once more than 8 characters are typed.
passwordEditText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (isPasswordValid(passwordEditText.getText())) {
passwordTextInput.setError(null); //Clear the error
}
return false;
}
});
return view;
}
COPY
Structure و Layout
در قسمت پیشین ، شما از دو جزء متریال (MDC) برای ساختن یک کاغذ ورود به کارگیری کردید: فیلدهای متنی و دکمه ها.
اینک اذن دهید این شالوده را با اضافه کردن ناوبری، ساختار و اطلاعات بسط دهیم.
درین نصیب از مغازه ما مستلزم دو نصیب دیگر هستیم که عبارتند از :
نوار نرمافزار رفیعتر
لیست کانال ای از تولیدها