java - 按键盘上的回车键应该重定向到 AlertDialog 中的 OK 按钮
问题描述
我正在尝试创建一个应用程序,其中将代码添加到自定义数组列表中。
用户在单击加号图标时输入代码。
我需要将用户限制为一行
我尝试使用android:maxLines="1
,但是当我按回车时它仍然不起作用。
我的问题是如何将 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);
}
}
}
这是我的 activity_main.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"
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>
解决方案
推荐阅读
- postgresql - 更新 jsonb 设置新值从同一个表中选择
- css - 实现 3 张图片精确大小的 CSS
- ios - 如何用 Swift 计算人生日的天数?
- android - 将请求分成 1 MB 的块(和 cursor.close())后仍然会发生 SQLiteBlobTooBigException
- android - 创建通知有什么区别
- c# - foreach 中嵌套 foreach 之后的代码未按预期执行
- javascript - Javascript中的视频播放器卡在隐藏/显示圈?
- sql - 从一个表中获取时间戳(显示在不同的行上)并将其显示在我的查询中
- python - 如何在 Python 中插入未排序的 2D numpy 数组并将插入的值与原始值进行比较?
- php - 如何在 GROUP_CONCAT MySQL 选择中有条件分隔符