当前位置:网站首页>Using activity to realize a simple inputable dialog box
Using activity to realize a simple inputable dialog box
2022-07-03 11:08:00 【Lindroy】
1、 Demand analysis
In the application, such comments are always indispensable , Some applications add one directly at the bottom EditText And a Button, Let the user input text or expression and click the button to submit ; And some are also placed EditText, But it's just a “ The decoration ”, No input function , After clicking it, the user will pop up a jump to a page that can be really edited or a dialog box that can input content . For example, the following effect :
The effect here can be subdivided into four points :
1. Click the button at the bottom and a dialog box will pop up , The dialog box is at the bottom of the layout ;
2. There is an input box in the dialog box EditText, You can enter content ;
3. After the dialog box pops up EditText Will automatically get the focus , Pop up the soft keyboard ;
4. The soft keyboard will top the dialog , Easy for users to edit .
At first I thought of PopupWindow, But because there is EditText, Interacting with a soft keyboard is a headache , So I switched to Activity. In this way, we can use Activity Also use this dialog , It's much more convenient . But after all, it's the same as what we usually use Activity It's different , Especially set its style , Otherwise, it is also a pile of pits .
2、 Dialog box Activity Layout and style of
Now let's start to implement the dialog we want . Build a new project ,MainActivity Just a supporting role , Just put a button at the bottom . Our main character is DialogActivity, Its layout is very simple , Just like usual Activity equally :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_gravity="bottom" android:background="@android:color/white" android:orientation="vertical" android:paddingLeft="10dp" android:paddingRight="10dp">
<EditText android:id="@+id/et_comment" android:layout_width="match_parent" android:layout_height="150dp" android:layout_marginTop="15dp" android:background="#f0f0f0" android:focusable="true" android:focusableInTouchMode="true" android:gravity="left|top" android:hint=" Let me say ~" android:paddingBottom="5dp" android:paddingLeft="8dp" android:paddingRight="8dp" android:paddingTop="5dp" android:textSize="14dp" />
<Button android:textColor="@android:color/white" android:background="@android:color/holo_blue_light" android:id="@+id/btn_submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:padding="5dp" android:text=" Comment " android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
The key is its style , Look at the code below :
<!-- You can enter the style of the dialog box -->
<style name="EditDialogStyle" parent="Theme.AppCompat.Light.NoActionBar"> // Set the background <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowSoftInputMode">adjustResize|stateHidden</item> //Dialog Of windowFrame The box is none <item name="android:windowFrame">@null</item> // Whether to display the title ,true Then remove the default title bar <item name="android:windowNoTitle">true</item> // Whether it appears in activity above ,false It will be covered by the soft keyboard <item name="android:windowIsFloating">true</item> // Is it translucent , by false The background is black and opaque <item name="android:windowIsTranslucent">true</item> // Whether there is coverage <item name="android:windowContentOverlay">@null</item> //Activity Animation effect of <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> // Whether the background is blurred , by false The effect is full transparency <item name="android:backgroundDimEnabled">true</item> // Whether to destroy when clicking the blank space Activity <item name="android:windowCloseOnTouchOutside">true</item> </style>
There are many properties to set , I have made comments , Just understand the function of each attribute , Let's talk about it in detail here . Don't forget , To the manifest file DialogActivity Use this theme :
<activity android:name=".DialogActivity" android:configChanges="orientation|screenSize" android:screenOrientation="portrait" android:theme="@style/EditDialogStyle"/>
Finally, I have to go Activity Set the width and height of the dialog box :
private void setWindow() {
// Window alignment screen width
Window win = this.getWindow();
win.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = win.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.BOTTOM;// Set the dialog box to display at the top
win.setAttributes(lp);
}
Run it , I believe you can see the effect .
3、 Automatically pop up the soft keyboard effect
We have made the dialog interface , But for a better user experience , We want to pop up the soft keyboard automatically when the dialog box appears . Here are two ways :
3.1、 Use InputMethodManager
Class display soft keyboard
We usually have to let someone EditText Get focus automatically pop-up soft keyboard can be written like this :
InputMethodManager inputManager =(InputMethodManager)etComment.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(etComment, 0);
But there is one thing to pay attention to : We want to make EditText Focus of attention , You must wait until the interface is drawn . So the delay is set 300ms Execute the code that pops up the soft keyboard , Give the interface time to draw :
new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
InputMethodManager inputManager =
(InputMethodManager) etComment.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(etComment, 0);
return false;
}
}).sendEmptyMessageDelayed(0, 300);
Add the code above , You can pop up the soft keyboard by yourself .
3.1、 Set up windowSoftInputMode
attribute
If you are careful, you will find that the front is DialogActivity There is a windowSoftInputMode
Property is not commented , Please forgive me for selling . This attribute is used to set the interaction mode between the window and the soft keyboard . It has many properties , You can refer to the reference articles given later . Here we use adjustResize
, Its function is to adjust the interface layout to leave enough space for the soft keyboard . that stateHidden
Well ? In fact, the soft keyboard doesn't pop up automatically, which is the ghost of it , It means that in general, the soft keyboard is hidden . We change it to another attribute :stateVisible
, It means that the soft keyboard is usually visible .
Let's run it again , The soft keyboard arrived as scheduled .
4、 Postscript
The effect we mentioned in the demand analysis has been achieved . Later, I also thought about adding custom animation effects to the dialog , But the animation when exiting is never set successfully , So if a reader realizes , Welcome to exchange and study . I saved the source code to the code cloud , If necessary, you can refer to :
You can enter the source code of the dialog
5、 Reference article
android:windowSoftInputMode Properties,
Get it all done Android Common problems of soft keyboard in development
边栏推荐
- Qt:qss custom qlistview instance
- Do you really need automated testing?
- QT:QSS自定义QToolBar和QToolBox实例
- Comment réaliser des tests automatisés pour les tests logiciels embarqués?
- 2021 postgraduate entrance examination mathematics 2 linear algebra
- Imread change image display size
- Typescript learning summary
- T5 的尝试
- “测试人”,有哪些厉害之处?
- Flink-- custom function
猜你喜欢
公司测试部门来了个00后卷王之王,老油条感叹真干不过,但是...
Qt:qss custom qpprogressbar instance
在职美团测试工程师的这八年,我是如何成长的,愿技术人看完都有收获
Comment réaliser des tests automatisés pour les tests logiciels embarqués?
我,大厂测试员,降薪50%去国企,后悔了...
Use ml.net+onnx pre training model to liven the classic "Huaqiang buys melons" in station B
Solution: jupyter notebook does not pop up the default browser
T5 attempt
T5 的尝试
redis那些事儿
随机推荐
测试理论概述
Game test related tests a hero's skills (spring moves are asked more questions)
Comment réaliser des tests automatisés pour les tests logiciels embarqués?
字节跳动大裁员,测试工程师差点遭团灭:大厂招人背后的套路,有多可怕?
Qt:qss custom qlineedit instance
Definition and properties of summation symbols
Flink -- built in function (all)
MAUI Developer Day in GCR
"Core values of testing" and "super complete learning guide for 0 basic software testing" summarized by test engineers for 8 years
17K薪资要什么水平?来看看95后测试工程师的面试全过程…
Tencent micro app to get wechat user information
Summary of the history of Mathematics
【Proteus仿真】74HC154 四线转12线译码器组成的16路流水灯
使用onvif协议操作设备
Probability theory: application of convolution in calculating moving average
QT: QSS custom qtableview instance
Qt:qss custom qspinbox instance
QT:QSS自定义 QProgressBar实例
IIS does not take effect after modifying the configuration information
2022 pinduogai 100000 sales tutorial