无法保存在SharedReferences值中,而是获取:d/inputmethodmanager:showsoftinput失败

yb3bgrhw  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(386)

我被困了几个小时,找不到一个有效的解决方案(至少不是我的水平),所以我请求你的帮助。再一次。我正在尝试将用户给定的一些值从编辑文本框保存到共享引用。出于某种原因,我接收的不是:1、5或我输入的任何值,而是: view:androidx.appcompat.widget.AppCompatEditText{b869139 VFED..CL. .F....ID 52,0-247,136 #7f0800a1 app:id/etTigPeZi} 同样的java代码也在其他应用程序中使用,而且工作正常。因为某种原因这里没有。我试着找出我可能犯的任何错误,但是,嗯,我没有发现任何错误。请看一看,如果你能救我!
谢谢您。祝大家新年快乐,顺便说一句,r.p。
在logcat中,详细:

2020-12-31 00:46:31.030 28160-28160/ro.pfeilmayer.since_stoppedsmoking D/InputMethodManager: showSoftInput fail, mServedView:DecorView@d9c9e35[Settings] view:androidx.appcompat.widget.AppCompatEditText{b869139 VFED..CL. .F....ID 52,0-247,136 #7f0800a1 app:id/etTigPeZi}
2020-12-31 00:46:31.040 28160-28160/ro.pfeilmayer.since_stoppedsmoking D/InputMethodManager: showSoftInput fail, mServedView:DecorView@d9c9e35[Settings] view:androidx.appcompat.widget.AppCompatEditText{97bf0df VFED..CL. .F....ID 82,160-217,296 #7f0800a0 app:id/etTigInPachet}
2020-12-31 00:46:31.060 28160-28160/ro.pfeilmayer.since_stoppedsmoking D/InputMethodManager: showSoftInput fail, mServedView:DecorView@d9c9e35[Settings] view:androidx.appcompat.widget.AppCompatEditText{8a3b12c VFED..CL. .F....ID 0,320-300,456 #7f08009f app:id/etCostPer

我的xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient_orange_red"
    tools:context=".Settings">

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@+id/imageView5"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout4">

        <TextView
            android:id="@+id/showTheDateSetted"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:gravity="center"
            android:hint="the day you quit"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/buttonSetTheDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="change"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/showTheDateSetted" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/buttonSetTheDate">

            <LinearLayout
                android:id="@+id/linearLayout2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="4dp"
                    android:layout_weight="2"
                    android:gravity="end"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/tvTigPeZi"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:layout_marginBottom="32dp"
                        android:text="Smoked daily : " />

                    <TextView
                        android:id="@+id/tvTigInPachet"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:layout_marginBottom="32dp"
                        android:text="Cigarettes/Pack : " />

                    <TextView
                        android:id="@+id/tvCostPerPachet"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:text="Cost/Pack : " />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/ValLayoutReset"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:orientation="vertical">

                    <EditText
                        android:id="@+id/etTigPeZi"
                        android:layout_width="65dp"
                        android:layout_height="wrap_content"
                        android:ems="1"
                        android:gravity="center"
                        android:hint="12"
                        android:inputType="number"
                        android:selectAllOnFocus="true"
                        android:textCursorDrawable="@null" />

                    <EditText
                        android:id="@+id/etTigInPachet"
                        android:layout_width="45dp"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:layout_marginBottom="8dp"
                        android:ems="1"
                        android:gravity="center"
                        android:hint="20"
                        android:inputType="number"
                        android:selectAllOnFocus="true"
                        android:textCursorDrawable="@null"
                        app:layout_constraintBottom_toTopOf="@+id/editTextNumberDecimal2"
                        app:layout_constraintTop_toBottomOf="@+id/editTextNumber"
                        app:layout_constraintVertical_bias="0.0"
                        />

                    <EditText
                        android:id="@+id/etCostPerPachet"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:ems="1"
                        android:gravity="center"
                        android:hint="7.58"
                        android:inputType="numberDecimal"
                        android:selectAllOnFocus="true"
                        android:textCursorDrawable="@null" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/tvTigPeZiSuffix"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="16dp"
                        android:layout_marginBottom="32dp"
                        android:text=" cigarettes" />

                    <TextView
                        android:id="@+id/tvTigInPachetSuffix"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="16dp"
                        android:layout_marginBottom="32dp"
                        android:text=" pcs" />

                    <TextView
                        android:id="@+id/tvCostPerPachetSuffix"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="16dp"
                        android:text=" currency" />

                </LinearLayout>

            </LinearLayout>

            <Button
                android:id="@+id/buttonSaveAll"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:text="Apply"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/linearLayout2" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </LinearLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingABBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/imageView5"
        app:srcCompat="@drawable/abc_vector_test" />

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="411dp"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:gravity="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="125dp"
            android:layout_height="125dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="10dp"
            app:layout_constraintBottom_toTopOf="@+id/linearLayout3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.186"
            app:srcCompat="@drawable/sicestoppedsmokig512x512" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/newbottom_devlogo_bk" />
</androidx.constraintlayout.widget.ConstraintLayout>

我的java:

import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Settings extends AppCompatActivity {

    SharedPreferences preferences;
    float x1, x2, y1, y2;

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

        preferences = getSharedPreferences("settings_values", MODE_PRIVATE);
        SharedPreferences settings = getSharedPreferences("settings_values", 0);

        TextView showDateResult = findViewById(R.id.showTheDateSetted);

        if (settings.getString("Date", null) == null) {
            handleDateButton();
        } else {

            String quitDateMem = preferences.getString("Date", "");

            showDateResult.setText("You quit smoking on : " + quitDateMem);

        }

        Button mButton = findViewById(R.id.buttonSetTheDate);

        //Hide Title Bar !
        getSupportActionBar().hide();

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleDateButton();
            }
        });

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.floatingABBack);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //Verificam data exista serati utilizator legat de fumat, daca nu, punem default
        //Luam din memorie setarile fumatorului si le afisam in textview

        String savedTigPeZi = preferences.getString("TigariZilnice", null);
        EditText tvEdTigPeZi = findViewById(R.id.etTigPeZi);
        tvEdTigPeZi.setText(savedTigPeZi);

        String savedTigPePachet = preferences.getString("TigariPerPachet", null);
        EditText tvEdTigPePachet = findViewById(R.id.etTigInPachet);
        tvEdTigPePachet.setText(savedTigPePachet);

        String savedCostPachet = preferences.getString("CostPachet", null);
        EditText tvEdCostPachet = findViewById(R.id.etCostPerPachet);
        tvEdCostPachet.setText(savedCostPachet);

        //Setari tigari pe zi, cost per pachet, continut pachet si definit campuri
        //Salvare date introduce in campurile de mai sus

        findViewById(R.id.buttonSaveAll)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(Settings.this);

                        builder.setTitle("Save settings");
                        builder.setMessage("Are you sure ?");

                        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // Do nothing but close the dialog

                                EditText etTigariPeZi = findViewById(R.id.etTigPeZi);
                                if (etTigariPeZi.getText().toString().length() == 0)
                                    etTigariPeZi.setError("Valid number is required !");
                                else {
                                    String stringTigariPeZi = etTigariPeZi.toString();
                                    SharedPreferences.Editor editorPezi = settings.edit();
                                    editorPezi.putString("TigariZilnice", stringTigariPeZi);
                                    editorPezi.apply();
                                    String saveTigariZilnice = preferences.getString("TigariZilnice", null);
                                    etTigariPeZi.setText(saveTigariZilnice);
                                    etTigariPeZi.requestFocus();
                                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                                    imm.showSoftInput(etTigariPeZi, InputMethodManager.SHOW_IMPLICIT);
                                    imm.hideSoftInputFromWindow(etTigariPeZi.getWindowToken(), 0);
                                }

                                EditText etTigariIntrunPachet = findViewById(R.id.etTigInPachet);
                                if (etTigariIntrunPachet.getText().toString().length() == 0)
                                    etTigariIntrunPachet.setError("Valid number is required !");
                                else {
                                    String stringTigariIntrunPachet = etTigariIntrunPachet.toString();
                                    SharedPreferences.Editor editorInPachet = settings.edit();
                                    editorInPachet.putString("TigariPerPachet", stringTigariIntrunPachet);
                                    editorInPachet.apply();
                                    String saveTigariPerPachet = preferences.getString("TigariPerPachet", null);
                                    etTigariIntrunPachet.setText(saveTigariPerPachet);
                                    etTigariIntrunPachet.requestFocus();
                                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                                    imm.showSoftInput(etTigariIntrunPachet, InputMethodManager.SHOW_IMPLICIT);
                                    imm.hideSoftInputFromWindow(etTigariIntrunPachet.getWindowToken(), 0);
                                }

                                EditText etCostPachet = findViewById(R.id.etCostPerPachet);
                                if (etCostPachet.getText().toString().length() == 0)
                                    etCostPachet.setError("Valid number is required !");
                                else {
                                    String stringCostPachet = etCostPachet.toString();
                                    SharedPreferences.Editor editorCostPachet = settings.edit();
                                    editorCostPachet.putString("CostPachet", stringCostPachet);
                                    editorCostPachet.apply();
                                    Context context = getApplicationContext();
                                    Toast.makeText(context, "New configuration was SAVED !", Toast.LENGTH_LONG).show();
                                    String saveCostPachet = preferences.getString("CostPachet", null);
                                    etCostPachet.setText(saveCostPachet);
                                    etCostPachet.requestFocus();
                                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                                    imm.showSoftInput(etCostPachet, InputMethodManager.SHOW_IMPLICIT);
                                    imm.hideSoftInputFromWindow(etCostPachet.getWindowToken(), 0);
                                }

                                dialog.dismiss();
                            }
                        });

                        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                // Do nothing
                                dialog.dismiss();
                            }
                        });

                        AlertDialog alert = builder.create();
                        alert.show();

                    }

                });

    }

    private void handleDateButton() {
        Calendar calendar = Calendar.getInstance();
        preferences = getSharedPreferences("settings_values", MODE_PRIVATE);
        SharedPreferences settings = getSharedPreferences("settings_values", 0);

        int an = calendar.get(Calendar.YEAR);
        int luna = calendar.get(Calendar.MONTH);
        int zi = calendar.get(Calendar.DATE);
        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

                TextView showDateResult = findViewById(R.id.showTheDateSetted);

                Calendar calendarFormatted = Calendar.getInstance();
                calendarFormatted.set(Calendar.YEAR, year);
                calendarFormatted.set(Calendar.MONTH, month);
                calendarFormatted.set(Calendar.DATE, dayOfMonth);

                CharSequence dateCharSequence = DateFormat.format("dd MM yyyy", calendarFormatted);
                showDateResult.setText("You quit smoking on : " + dateCharSequence);

                SharedPreferences.Editor editor = settings.edit();
                String prefData = dateCharSequence.toString();

                SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
                Date date = new Date();
                String secondDate = myFormat.format(date);        //azi

                try {
                    Date startDate = myFormat.parse(prefData);
                    Date endDate = myFormat.parse(secondDate);
                    long diff = endDate.getTime() - startDate.getTime();

                    if (diff < 0) {

                        editor.putString("Date", secondDate);
                        editor.apply();
                        showDateResult.setText("You quit smoking on : " + secondDate);
                        Context context = getApplicationContext();
                        Toast.makeText(context, "Can't be in the future", Toast.LENGTH_SHORT).show();

                    } else {
                        editor.putString("Date", prefData);
                        editor.apply();
                    }

                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

        }, an, luna, zi);

        datePickerDialog.show();

    }

    public boolean onTouchEvent(MotionEvent touchevent) {
        switch (touchevent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x1 = touchevent.getX();
                y1 = touchevent.getY();
                break;
            case MotionEvent.ACTION_UP:
                ;
                x2 = touchevent.getX();
                y2 = touchevent.getY();
                if (x1 < x2) {
                    Intent i = new Intent(Settings.this, MainActivity.class);
                    startActivity(i);
                }
                break;
        }
        return false;
    }

}```
6ss1mwsb

6ss1mwsb1#

要获取edittext中的字符串,请使用edittext.gettext()而不是edittext.tostring()。第一个获取用户(或应用程序)输入的文本。第二个获取编辑文本对象的字符串表示形式。

相关问题