EditText不接受DialogFragment中键盘的“数字”输入

时间:2017-10-17 06:04:14

标签: android

我正在使用EditText以数字(频率)的形式捕获用户输入。我在DialogFragment中有一个EditText。但正如你在下面的gif链接中看到的那样,带有'android:inputType = number'的EditText不接受来自softkeybard的数字输入。请帮忙!

enter image description here

这是布局文件的代码 -

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:text="@string/add_dialog_title"
        android:textColor="@android:color/white"
        android:textSize="22sp" />

    <TextView
        android:id="@+id/text_view_name"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:inputType="textCapWords"
        android:text="@string/habit_name"
        android:textSize="20sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/habit_name"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_weight="1"
        android:inputType="textCapWords" />

    <TextView
        android:id="@+id/text_view_start_date"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:text="@string/start_date"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:layout_weight="1"
        android:textSize="20sp"
        tools:text="@string/start_date" />

    <TextView
        android:id="@+id/text_view_number_of_times"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:text="@string/times"
        android:textSize="20sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/number_of_times"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_weight="1"
        android:inputType="number" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="40dp"
        android:layout_weight="1"
        android:gravity="center">

        <Button
            android:id="@+id/save_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/save" />

    </LinearLayout>

</LinearLayout>

以下是此DialogFragment的Java代码 -

import android.app.Dialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v4.app.DialogFragment;

import android.text.TextUtils;
import android.text.method.TextKeyListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import iamutkarshtiwari.github.io.habtrac.R;
import iamutkarshtiwari.github.io.habtrac.activity.models.HabitContract;
import iamutkarshtiwari.github.io.habtrac.activity.utils.HabitDbHelper;

public class AddHabitFragment extends DialogFragment {

    String mImageURI;

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();

        final View addView = inflater.inflate(R.layout.dialog_add, null);


        final Dialog addDialog = builder.setView(addView)
                .create();



        addDialog.setOnKeyListener(new Dialog.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface arg0, int keyCode,
                                 KeyEvent event) {
                // TODO Auto-generated method stub
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    AddHabitFragment.this.getDialog().cancel();
//                    finish();
//                    dialog.dismiss();
                }
                return true;
            }
        });

        // Current date
        Calendar c = Calendar.getInstance();
        System.out.println("Current time => " + c.getTime());
        SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
        String formattedDate = df.format(c.getTime());
        final TextView startDate = (TextView) addView.findViewById(R.id.date);
        startDate.setText(formattedDate);


        // set on the listener for the positive button of the dialog
        addDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {

                Button saveButton = (Button) addView.findViewById(R.id.save_button);

                saveButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // add wantToCloseDialog to prevent the dialog from closing when the information is not completely filled out
                        Boolean wantToCloseDialog = false;

                        EditText editHabitName = (EditText) addView.findViewById(R.id.habit_name);

                        EditText editNumberOfTimes = (EditText) addView.findViewById(R.id.number_of_times);

                        String name = editHabitName.getText().toString().trim();
                        String date = startDate.getText().toString().trim();
                        String frequency = editNumberOfTimes.getText().toString().trim();

                        // validate all the required infomation
                        if (TextUtils.isEmpty(name)  || TextUtils.isEmpty(date) || TextUtils.isEmpty(frequency)) {
                            Toast.makeText(getActivity(), getString(R.string.habit_info_not_complete), Toast.LENGTH_SHORT).show();
                        }
                        else {
                            insertHabit(name, date, frequency);
                            wantToCloseDialog = true;
                        }

                        // after successfully inserting product, dismiss the dialog
                        if(wantToCloseDialog)
                            addDialog.dismiss();
                    }
                });
            }
        });

        return addDialog;
    }

    private void insertHabit(String name, String date, String frequency){
        HabitDbHelper mHabitDbHelper = new HabitDbHelper(getActivity());
        SQLiteDatabase db = mHabitDbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(HabitContract.HabitEntry.COLUMN_NAME, name);
        values.put(HabitContract.HabitEntry.COLUMN_START_DATE, date);
        values.put(HabitContract.HabitEntry.COLUMN_NUMBER_OF_TIMES, frequency);

        long newRowId = db.insert(HabitContract.HabitEntry.TABLE_NAME, null, values);

        if (newRowId == -1) {
            // If the row ID is -1, then there was an error with insertion.
            Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.save_error), Toast.LENGTH_SHORT).show();
        } else {
            // Otherwise, the insertion was successful and we can display a toast with the row ID.
            Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.habit_saved, newRowId), Toast.LENGTH_SHORT).show();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

android:weightSum:"8"中取LinearLayout。这将解决你的问题。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum:"8"
android:orientation="vertical">

.......

</LinearLayout>