Android手柄软输入键

时间:2016-05-06 20:16:41

标签: java android listview android-edittext

我正在android上构建一个非常简单的列表应用程序,其中我有一个用于输入的edittext,一个用于存储已保存输入的listview,以及一个用于将文本从edittext推送到listview的按钮。现在我正在尝试处理错误键,因此它不仅不会在按下时创建新行,而且还会在我单击“输入”按钮时将文本发送到列表视图。

我将edittext设置为单行以防止新行,并且我在我的edittext中添加了一个onEditorActionListener来处理keyevents。这是我的代码。

activity_main.xml中

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/myEditText"
    android:imeOptions="actionSearch|actionGo"
    android:hint="Enter a new item"
    android:singleLine="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toLeftOf="@+id/myImageButton"
    android:layout_toStartOf="@+id/myImageButton"/>

MainActivity.java

public class MainActivity extends AppCompatActivity {
// Declare our View variables
private ListView mListView;
private EditText mEditText;
private Button mDoneButton;
private ImageButton mImageButton;
private static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Assign the view from the layout file to the corresponding variables
    mListView = (ListView) findViewById(R.id.myListView);
    mEditText = (EditText) findViewById(R.id.myEditText);
    mDoneButton = (Button) findViewById(R.id.myDoneButton);
    mImageButton = (ImageButton) findViewById(R.id.myImageButton);


    // Listview items adapter
    items = new ArrayList<>();
    readItems();
    itemsAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, items);
    mListView.setAdapter(itemsAdapter);

    //setup enter listener
    mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                //handle search key click
                onAddItem(v);
                Log.d(TAG, "Handled!");
                return true;
            }
            if (actionId == EditorInfo.IME_ACTION_GO) {
                //Handle go key click
                onAddItem(v);
                return true;
            }
            return handled;
        }
    });
}

//Add item to list from either the button or enter
public void onAddItem(View v) {
    String itemText = mEditText.getText().toString();
    itemsAdapter.add(itemText);
    mEditText.setText("");

    writeItems();
}

代码给我带来麻烦。我甚至没有从log.d中得到任何东西,但代码中的其他所有东西,除了这个监听器,都按预期工作。

        //setup enter listener
    mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                //handle search key click
                onAddItem(v);
                Log.d(TAG, "Handled!");
                return true;
            }
            if (actionId == EditorInfo.IME_ACTION_GO) {
                //Handle go key click
                onAddItem(v);
                return true;
            }
            return handled;
        }
    });

1 个答案:

答案 0 :(得分:1)

要使用IME_ACTION_SEARCH和其他人,您还需要在xml-layout中指定这些操作:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/myEditText"
    android:imeOptions="actionSearch|others"
    ...
    />