Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按键盘上的“回车”按钮应重定向到AlertDialog中的“确定”按钮_Java_Android - Fatal编程技术网

Java 按键盘上的“回车”按钮应重定向到AlertDialog中的“确定”按钮

Java 按键盘上的“回车”按钮应重定向到AlertDialog中的“确定”按钮,java,android,Java,Android,我正在尝试创建一个应用程序,其中代码被添加到自定义数组列表中 用户在单击加号图标时输入代码 我需要将用户限制为一行 我尝试使用android:maxLines=“1,但当我按enter键时,它仍然不起作用 我的问题是如何将ENTER按钮重定向到AlertDialog的OK按钮 以下是我的主要活动: import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androi

我正在尝试创建一个应用程序,其中代码被添加到自定义数组列表中

用户在单击加号图标时输入代码

我需要将用户限制为一行

我尝试使用android:maxLines=“1,但当我按enter键时,它仍然不起作用

我的问题是如何将ENTER按钮重定向到AlertDialog的OK按钮

以下是我的主要活动:


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import android.os.Bundle;

import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

    Boolean itIsEmpty = Boolean.FALSE;
    EditText editText;
    private static final String TAG = "Main Activity";
    ArrayList<CodeId> codeArray = new ArrayList<>();
    CodeIdListAdapter arrayAdapter;

    public void addCodeReal(){
        CodeId newCodeId = new CodeId(editText.getText().toString());
        codeArray.add(newCodeId);
        arrayAdapter.notifyDataSetChanged();
        saveData();
        Log.d(TAG, "onClick: " + newCodeId.toString());
    }


    public void addCode (View view){



        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Enter code to add").setMessage("Search below to find and add the code");

        final View customLayout = getLayoutInflater().inflate(R.layout.alert_dialog,null);

        builder.setView(customLayout);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                editText = customLayout.findViewById(R.id.editText);
                editText.setOnKeyListener(new View.OnKeyListener() {
                    @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        if((event.getAction() == KeyEvent.ACTION_DOWN) && (event.getAction() == KeyEvent.KEYCODE_ENTER)){

                            if (editText.getText().toString() == ""){
                                return false;
                            }else{
                                addCodeReal();
                            }

                        }
                        return false;
                    }
                });
                addCodeReal();
            }
        });

        builder.setNegativeButton("Cancel", null);

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


    }

    public void saveData()  {
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Gson gson = new Gson();
        String json = gson.toJson(codeArray);
        editor.putString("task list", json);
        editor.apply();
        Log.d(TAG, "saveData: " + codeArray.size());

    }

   private void loadData() {
       SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
       Gson gson = new Gson();
       String json = sharedPreferences.getString("task list", null);
       Type type = new TypeToken<ArrayList<CodeId>>() {}.getType();
       ArrayList<CodeId> mCodeArray;
       mCodeArray = gson.fromJson(json, type);
       if (mCodeArray == null) {
           mCodeArray = new ArrayList<>();
       }
       for(int i = 0; i< mCodeArray.size();i++){
           Log.d(TAG, "loadData: " + mCodeArray.get(i));
           codeArray.add(mCodeArray.get(i));
           arrayAdapter.notifyDataSetChanged();
       }
       Log.d(TAG, "loadData: " + codeArray.size());


    }

    private void deleteData(){
        SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
        sharedPreferences.getString("task list", null);
        Editor prefsEditor = sharedPreferences.edit();
        prefsEditor.remove("task list");
        while(itIsEmpty){
            int i = 0;
            i++;
            if(prefsEditor.equals(null)){
                Log.d(TAG, "deleteData: if part" + codeArray.get(i));
                itIsEmpty = Boolean.TRUE;
            }else{
                Log.d(TAG, "deleteData: else part" + codeArray.get(i));
                prefsEditor.remove("task list");
            }
        }
        prefsEditor.commit();
        Log.d(TAG, "deleteData:  after clear");
        codeArray.clear();
        Log.d(TAG, "deleteData: before clear");

        arrayAdapter.notifyDataSetChanged();
    }

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

        ListView listView = findViewById(R.id.listView);

        Toolbar toolbar = findViewById(R.id.toolbar_main);
        setSupportActionBar(toolbar);

        Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
                .getBoolean("isFirstRun", true);


        if (isFirstRun) {
            //show start activity

            startActivity(new Intent(MainActivity.this, login_activity.class));

        }


        getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
                .putBoolean("isFirstRun", false).apply();

        arrayAdapter = new CodeIdListAdapter(this,codeArray);

        //ArrayAdapter arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1, codeList);

        listView.setAdapter(arrayAdapter);

        Log.d(TAG, "onCreate: loadData func working");
        loadData();


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.front_page_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_del:
                deleteData();
                Toast.makeText(this, "Cleared!", Toast.LENGTH_SHORT).show();
            default:
                return super.onOptionsItemSelected(item);
        }

    }
}

导入androidx.annotation.NonNull;
导入androidx.appcompat.app.appcompat活动;
导入android.app.AlertDialog;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.content.SharedReferences.Editor;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.KeyEvent;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.EditText;
导入android.widget.ListView;
导入android.widget.Toast;
导入androidx.appcompat.widget.Toolbar;
导入com.google.gson.gson;
导入com.google.gson.reflect.TypeToken;
导入java.lang.reflect.Type;
导入java.util.ArrayList;
公共类MainActivity扩展了AppCompatActivity{
Boolean itIsEmpty=Boolean.FALSE;
编辑文本编辑文本;
私有静态最终字符串TAG=“Main Activity”;
ArrayList codeArray=新的ArrayList();
CodeIdListAdapter阵列适配器;
public void addCodeReal(){
CodeId newCodeId=newCodeId(editText.getText().toString());
add(newCodeId);
arrayAdapter.notifyDataSetChanged();
saveData();
d(标记“onClick:+newCodeId.toString());
}
公共无效添加代码(视图){
AlertDialog.Builder=新建AlertDialog.Builder(此);
builder.setTitle(“输入要添加的代码”).setMessage(“在下面搜索以查找和添加代码”);
最终视图customLayout=GetLayoutFlater().充气(R.layout.alert_对话框,空);
builder.setView(自定义布局);
setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
editText=customLayout.findViewById(R.id.editText);
editText.setOnKeyListener(新视图.OnKeyListener(){
@凌驾
公共布尔onKey(视图v、int keyCode、KeyEvent事件){
如果((event.getAction()==KeyEvent.ACTION\u DOWN)&&(event.getAction()==KeyEvent.KEYCODE\u ENTER)){
如果(editText.getText().toString()==“”){
返回false;
}否则{
addCodeReal();
}
}
返回false;
}
});
addCodeReal();
}
});
builder.setNegativeButton(“取消”,null);
AlertDialog=builder.create();
dialog.show();
}
公共void saveData(){
SharedPreferences SharedPreferences=getSharedPreferences(“共享首选项”,模式\私人);
SharedReferences.Editor=SharedReferences.edit();
Gson Gson=新的Gson();
字符串json=gson.toJson(codeArray);
putString(“任务列表”,json);
editor.apply();
Log.d(标记“saveData:+coderaray.size());
}
私有void loadData(){
SharedPreferences SharedPreferences=getSharedPreferences(“共享首选项”,模式\私人);
Gson Gson=新的Gson();
String json=SharedReferences.getString(“任务列表”,null);
Type Type=new-TypeToken(){}.getType();
ArrayList-mCodeArray;
mCodeArray=gson.fromJson(json,类型);
if(mCodeArray==null){
mCodeArray=newarraylist();
}
对于(int i=0;i<?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"
    tools:context=".MainActivity">



    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton3"
        android:layout_width="109dp"
        android:layout_height="58dp"
        android:layout_marginStart="286dp"
        android:layout_marginTop="596dp"
        android:layout_marginEnd="11dp"
        android:layout_marginBottom="25dp"
        android:clickable="true"
        android:onClick="addCode"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.33"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.19"
        app:srcCompat="@android:drawable/ic_input_add" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="368dp"
        android:layout_height="613dp"
        android:layout_marginTop="39dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar_main"
        android:layout_width="411dp"
        android:layout_height="51dp"
        android:layout_marginBottom="21dp"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintBottom_toTopOf="@+id/listView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>