mirror of https://github.com/FongMi/TV.git
parent
fdbc1818f6
commit
bccf9d005c
@ -0,0 +1,6 @@ |
||||
package com.fongmi.android.tv.impl; |
||||
|
||||
public interface PassCallback { |
||||
|
||||
void setPass(String pass); |
||||
} |
||||
@ -0,0 +1,94 @@ |
||||
package com.fongmi.android.tv.ui.custom.dialog; |
||||
|
||||
import android.app.Dialog; |
||||
import android.content.DialogInterface; |
||||
import android.os.Bundle; |
||||
import android.view.KeyEvent; |
||||
import android.view.LayoutInflater; |
||||
import android.view.View; |
||||
import android.view.ViewGroup; |
||||
import android.view.WindowManager; |
||||
import android.view.inputmethod.EditorInfo; |
||||
import android.widget.FrameLayout; |
||||
import android.widget.TextView; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.annotation.Nullable; |
||||
import androidx.fragment.app.Fragment; |
||||
import androidx.fragment.app.FragmentActivity; |
||||
|
||||
import com.fongmi.android.tv.databinding.DialogPassBinding; |
||||
import com.fongmi.android.tv.impl.PassCallback; |
||||
import com.fongmi.android.tv.utils.ResUtil; |
||||
import com.google.android.material.bottomsheet.BottomSheetBehavior; |
||||
import com.google.android.material.bottomsheet.BottomSheetDialog; |
||||
import com.google.android.material.bottomsheet.BottomSheetDialogFragment; |
||||
|
||||
public class PassDialog extends BottomSheetDialogFragment { |
||||
|
||||
private final PassCallback callback; |
||||
private DialogPassBinding binding; |
||||
|
||||
public static void show(FragmentActivity activity) { |
||||
for (Fragment fragment : activity.getSupportFragmentManager().getFragments()) if (fragment instanceof BottomSheetDialogFragment) return; |
||||
new PassDialog(activity).show(activity.getSupportFragmentManager(), null); |
||||
} |
||||
|
||||
private PassDialog(FragmentActivity activity) { |
||||
this.callback = (PassCallback) activity; |
||||
} |
||||
|
||||
@Nullable |
||||
@Override |
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
||||
binding = DialogPassBinding.inflate(inflater, container, false); |
||||
return binding.getRoot(); |
||||
} |
||||
|
||||
@NonNull |
||||
@Override |
||||
public Dialog onCreateDialog(Bundle savedInstanceState) { |
||||
BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState); |
||||
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); |
||||
dialog.setOnShowListener((DialogInterface f) -> setBehavior(dialog)); |
||||
return dialog; |
||||
} |
||||
|
||||
private void setBehavior(BottomSheetDialog dialog) { |
||||
WindowManager.LayoutParams params = dialog.getWindow().getAttributes(); |
||||
FrameLayout bottomSheet = dialog.findViewById(com.google.android.material.R.id.design_bottom_sheet); |
||||
BottomSheetBehavior<FrameLayout> behavior = BottomSheetBehavior.from(bottomSheet); |
||||
behavior.setState(BottomSheetBehavior.STATE_EXPANDED); |
||||
params.width = ResUtil.dp2px(250); |
||||
dialog.getWindow().setAttributes(params); |
||||
behavior.setSkipCollapsed(true); |
||||
} |
||||
|
||||
@Override |
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
||||
super.onViewCreated(view, savedInstanceState); |
||||
initEvent(); |
||||
} |
||||
|
||||
protected void initEvent() { |
||||
binding.positive.setOnClickListener(this::onPass); |
||||
binding.pass.setOnEditorActionListener(this::onEditorAction); |
||||
} |
||||
|
||||
private boolean onEditorAction(TextView view, int actionId, KeyEvent event) { |
||||
if (actionId == EditorInfo.IME_ACTION_DONE) binding.positive.performClick(); |
||||
return true; |
||||
} |
||||
|
||||
private void onPass(View view) { |
||||
String pass = binding.pass.getText().toString().trim(); |
||||
if (pass.length() > 0) callback.setPass(pass); |
||||
dismiss(); |
||||
} |
||||
|
||||
@Override |
||||
public void onDestroyView() { |
||||
super.onDestroyView(); |
||||
binding = null; |
||||
} |
||||
} |
||||
@ -0,0 +1,40 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:background="@color/white" |
||||
android:fitsSystemWindows="true" |
||||
android:gravity="center" |
||||
android:orientation="horizontal" |
||||
android:paddingTop="4dp" |
||||
android:paddingBottom="4dp"> |
||||
|
||||
<com.google.android.material.textfield.TextInputEditText |
||||
android:id="@+id/pass" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_margin="16dp" |
||||
android:layout_weight="1" |
||||
android:background="@null" |
||||
android:hint="@string/live_pass" |
||||
android:imeOptions="actionDone" |
||||
android:inputType="textPassword" |
||||
android:maxLength="20" |
||||
android:singleLine="true" |
||||
android:textSize="16sp" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/positive" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginEnd="16dp" |
||||
android:background="@drawable/selector_text" |
||||
android:focusable="true" |
||||
android:focusableInTouchMode="true" |
||||
android:gravity="center" |
||||
android:singleLine="true" |
||||
android:text="@string/dialog_positive" |
||||
android:textColor="@color/white" |
||||
android:textSize="14sp" /> |
||||
|
||||
</LinearLayout> |
||||
Loading…
Reference in new issue