Click a blank space in the Activity of the custom Dialog style in Andriod to hide the software disk (the Dialog does not disappear) and the andrioddialog

Source: Internet
Author: User

Click a blank space in the Activity of the custom Dialog style in Andriod to hide the software disk (the Dialog does not disappear) and the andrioddialog
I. Demand triggering scenarios:

The Dialog with EditText must be displayed in the project. When EditText is edited, you must click the blank space of Dilog to hide the soft keyboard. But the Dialog will not disappear. Example:

II. Implementation Method: I personally thought about directly creating a Dialog using the new method when releasing a requirement. After several attempts, the requirements cannot be met, therefore, the Activity is displayed as a Dialog style, and the call method is required. The specific implementation is as follows:

 

2.1AndroidMainfest.xml the configuration file needs to add a dialog topic to the activity that needs to be displayed as a dialog style in the configuration file (You can also add a custom topic when using the System)
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.panhouye.dialoginput">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name=".Main2Activity"           android:theme="@style/Base.V7.Theme.AppCompat.Light.Dialog"></activity>    </application></manifest>
2.2Java implementation code (1) BaseActivity is used as the base class of the Activity in this Demo. The code implementation is as follows:
package com.panhouye.dialoginput;import android.content.Context;import android.support.v7.app.AppCompatActivity;import android.view.MotionEvent;import android.view.inputmethod.InputMethodManager;/** * Created by pamhouye on 2017/4/25 0025. */public class BaseActivity extends AppCompatActivity {    @Override    public boolean onTouchEvent(MotionEvent event) {        // TODO Auto-generated method stub        if(event.getAction() == MotionEvent.ACTION_DOWN){            if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){                InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);            }        }        return super.onTouchEvent(event);    }}

(2) MainActivity implementation code is as follows:

Package com. panhouye. dialoginput; import android. content. intent; import android. OS. bundle; import android. view. view; public class MainActivity extends BaseActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} // The dialog button trigger event Method public void dialog (View view) {Intent intent = new Intent (this, Main2Activity. class); startActivity (intent );}}

(3) The Main2Activity implementation code is as follows:

Package com. panhouye. dialoginput; import android. OS. bundle; import android. view. gravity; import android. view. window; import android. view. windowManager; public class Main2Activity extends BaseActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main2); // set 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. CENTER; // set the dialog box to display win. setAttributes (lp); // you can click an external blank space to close Activity this. setFinishOnTouchOutside (true );}}
So it's just superficial.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.