警报对话框编辑文本软键盘隐藏警报对话框的内容

时间:2018-03-20 09:19:18

标签: android android-edittext android-alertdialog android-softkeyboard

我是Android开发的初学者。当我们点击主活动中的'textView'时,此应用程序使用'CustomDialog'显示两个'EditText'和一个'Spinner'。当我们单击该对话框中的EditText时,软键盘出现并隐藏该对话框内容的一部分。请帮我解决这个问题。 Android版本7.0会出现此问题,而不是法律版本。

我的代码在下方:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.threeiteam.weightstripz">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <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"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="adjustNothing">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite">

        <LinearLayout
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@drawable/linear_bottom_border"
            android:layout_marginTop="20dp"
            android:id="@+id/measurement_height_layout">

            <TextView
                android:id="@+id/measurement_height_text_view1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:hint="@string/hint_measurement_height_feet"
                android:textSize="@dimen/medium_font_size"/>

            <TextView
                android:id="@+id/measurement_unit_text_view1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ft,"
                android:textSize="@dimen/medium_font_size"
                android:layout_marginLeft="3dp"/>

            <TextView
                android:id="@+id/measurement_height_text_view2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:hint="@string/hint_measurement_height_inches"
                android:textSize="@dimen/medium_font_size"
                android:layout_marginLeft="3dp"/>

            <TextView
                android:id="@+id/measurement_unit_text_view2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="in"
                android:textSize="@dimen/medium_font_size"
                android:layout_marginLeft="3dp"/>

        </LinearLayout>

</LinearLayout>

当我点击名为@ + id / measurement_height_layout的'Linearlayout时,会出现对话框。

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="20dp"
        android:id="@+id/tittle_text_view"
        android:textColor="@color/colorBlack"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp">

        <EditText
            android:id="@+id/edit_text_first"
            android:layout_width="70dp"
            android:layout_height="match_parent"
            android:hint="0"
            android:inputType="number" />

        <TextView
            android:id="@+id/text_view_first"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:gravity="center"/>

        <EditText
            android:id="@+id/edit_text_second"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:hint="0"
            android:layout_marginLeft="5dp"
            android:inputType="number" />

        <TextView
            android:id="@+id/text_view_second"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:gravity="center"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/error_text_view"
            android:textSize="13dp"
            android:textColor="@color/colorOrange"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp">

        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/Base.Widget.AppCompat.Spinner.Underlined"
            android:id="@+id/height_spinner">

        </Spinner>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:layout_marginRight="40dp"
        android:layout_marginTop="20dp">

        <TextView
            android:id="@+id/height_set_button"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Set"
            android:gravity="end"/>

    </LinearLayout>

</LinearLayout>

我在

中显示了custom_dialog

MainActivity.java

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @InjectView(R.id.measurement_back_button)
    ImageView measurementBackButton;

    @InjectView(R.id.measurement_next_text_view)
    TextView measurementNextButton;

    @InjectView(R.id.measurement_height_text_view1)
    TextView measurementHeightTextView1;

    @InjectView(R.id.measurement_height_text_view2)
    TextView measurementHeightTextView2;

    @InjectView(R.id.measurement_weight_text_view)
    TextView measurementWeightTextView;

    @InjectView(R.id.measurement_height_layout)
    LinearLayout measurementHeightLayout;

    @InjectView(R.id.measurement_unit_text_view1)
    TextView feetTextView;

    @InjectView(R.id.measurement_unit_text_view2)
    TextView inchesTextView;

    @InjectView(R.id.measurement_weight_unit_text_view)
    TextView weightUnitTextView;

    private Context context;
    ArrayAdapter<String> tagAdapter;
    private static int heightFeet = 0;
    private static int heightInches = 0;
    private static double heightCenti = 0;
    private static double weightKilo = 0;
    private static double weightPounds = 0;
    private static int weightStone = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up_measurement);

        context = this;
        ButterKnife.inject(this);

        if(Build.VERSION.SDK_INT>=21) {
            Window window = this.getWindow();

            // clear FLAG_TRANSLUCENT_STATUS flag:
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

            // finally change the color
            window.setStatusBarColor(ContextCompat.getColor(this, R.color.colorDarkBlue));
        }
    }

    @OnClick(R.id.measurement_height_layout)
    public void getValidHeightValue(){        

            displayHeightDialog("Height", "", "");

    }


    public void displayHeightDialog(String tittle, String feet, String inches){

        if (measurementHeightTextView1.getText().toString().trim().length()>0 && !measurementHeightTextView1.getText().toString().trim().equals("0")){

            heightCenti = 0;

            final Dialog dialog = new Dialog(SignUpMeasurementActivity.this);
            //dialog.setTitle(height);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

            dialog.setContentView(R.layout.measurement_custom_dialog);

            if (Build.VERSION.SDK_INT >= 21) {
                dialog.getWindow().setLayout(1200, 1200);
            } else {
                dialog.getWindow().setLayout(400, 400);
            }

            List<String> itemArray = new ArrayList<String>();
            itemArray.add("Feet & Inches");
            itemArray.add("Centimeters");

            final TextView tittleTextView = (TextView) dialog.findViewById(R.id.tittle_text_view);
            final EditText firstEditText = (EditText) dialog.findViewById(R.id.edit_text_first);
            final TextView firstTextView = (TextView) dialog.findViewById(R.id.text_view_first);
            final EditText secondEditText = (EditText) dialog.findViewById(R.id.edit_text_second);
            final TextView secondTextView = (TextView) dialog.findViewById(R.id.text_view_second);
            final Spinner heightSpinner = (Spinner) dialog.findViewById(R.id.height_spinner);
            final TextView setButton = (TextView) dialog.findViewById(R.id.height_set_button);
            final TextView errorTextView = (TextView) dialog.findViewById(R.id.error_text_view);

//            /firstEditText.setVisibility(View.GONE);
//            firstTextView.setVisibility(View.GONE);
//            secondEditText.setVisibility(View.GONE);
//            secondTextView.setVisibility(View.GONE);
            firstEditText.setText(feet);
            secondEditText.setText(inches);
            firstTextView.setText("ft");
            secondTextView.setText("in");
            errorTextView.setVisibility(View.GONE);
            setButton.setTextColor(getResources().getColor(R.color.colorDarkGreen));
            tittleTextView.setText(tittle);

            tagAdapter = new ArrayAdapter<String>(context, R.layout.support_simple_spinner_dropdown_item, itemArray);
            heightSpinner.setAdapter(tagAdapter);
            heightSpinner.setSelection(0);

            dialog.show();

            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

//            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

//            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
//                @Override
//                public void onShow(DialogInterface dialog) {
//                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//                    imm.showSoftInput(firstEditText, InputMethodManager.SHOW_IMPLICIT);
//                }
//            });

        }else {

            heightCenti = 0;

            final Dialog dialog = new Dialog(SignUpMeasurementActivity.this);
            //dialog.setTitle(height);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.measurement_custom_dialog);

            if (Build.VERSION.SDK_INT >= 21) {
                dialog.getWindow().setLayout(1200, 1200);
            } else {
                dialog.getWindow().setLayout(400, 400);
            }

            List<String> itemArray = new ArrayList<String>();
            itemArray.add("Feet & Inches");
            itemArray.add("Centimeters");

            final TextView tittleTextView = (TextView) dialog.findViewById(R.id.tittle_text_view);
            final EditText firstEditText = (EditText) dialog.findViewById(R.id.edit_text_first);
            final TextView firstTextView = (TextView) dialog.findViewById(R.id.text_view_first);
            final EditText secondEditText = (EditText) dialog.findViewById(R.id.edit_text_second);
            final TextView secondTextView = (TextView) dialog.findViewById(R.id.text_view_second);
            final Spinner heightSpinner = (Spinner) dialog.findViewById(R.id.height_spinner);
            final TextView setButton = (TextView) dialog.findViewById(R.id.height_set_button);
            final TextView errorTextView = (TextView) dialog.findViewById(R.id.error_text_view);

            firstEditText.setVisibility(View.GONE);
            firstTextView.setVisibility(View.GONE);
            secondEditText.setVisibility(View.GONE);
            secondTextView.setVisibility(View.GONE);
            errorTextView.setVisibility(View.GONE);
            setButton.setTextColor(getResources().getColor(R.color.colorDarkGreen));
            tittleTextView.setText(tittle);

            tagAdapter = new ArrayAdapter<String>(context, R.layout.support_simple_spinner_dropdown_item, itemArray);
            heightSpinner.setAdapter(tagAdapter);
            heightSpinner.setSelection(0);

            dialog.show();

            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

//            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

//            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
//                @Override
//                public void onShow(DialogInterface dialog) {
//                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//                    imm.showSoftInput(firstEditText, InputMethodManager.SHOW_IMPLICIT);
//                }
//            });
        }
    }
}

0 个答案:

没有答案