簡體   English   中英

Android處理軟輸入鍵

[英]Android handle soft enter key

我在android上構建了一個非常簡單的列表應用程序,其中有用於輸入的edittext,用於存儲保存的輸入的listview和用於將文本從edittext推送到listview的按鈕。 現在,我正在嘗試處理錯誤鍵,因此它不僅不會在印刷時創建新行,而且還可以在單​​擊Enter鍵時將其發送到列表視圖。

我將edittext設置為singleline以防止出現新行,並且在我的edittext中添加了onEditorActionListener來處理按鍵事件。 這是我的代碼。

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;
        }
    });

為了使用IME_ACTION_SEARCH和其他,您還需要在xml-layout中指定以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM