待办事项例如我不工作 [英] To do list example doesn't work for me

查看:114
本文介绍了待办事项例如我不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的例子不适合我的工作方式。由于每code,它应该添加在ListView的条目时ENTER键是pressed。但它只是进入的领域的EditText新的生产线并没有真正增加在ListView的条目。运行应用程序时我没有得到任何错误。
这里是我的活动和XML文件。

The following example doesn't work in the right way for me. As per the code, it should add the entry in the ListView when "ENTER" key is pressed. But it just enters the new line in the EditText field and doesn't really add the entry in the ListView. I don't get any error while running the application. Here's my Activity and XML file.

MainActivity.java

MainActivity.java

package com.example.todo_list;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Inflate your view
setContentView(R.layout.activity_main);

// Get references to UI widgets
ListView myListView = (ListView)findViewById(R.id.myListView);
final EditText myEditText = (EditText)findViewById(R.id.myEditText);

// Create the array list of to do items
final ArrayList<String> todoItems = new ArrayList<String>();
// Create the array adapter to bind the array to the listview
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this,
                              android.R.layout.simple_list_item_1,
                              todoItems);
// Bind the array adapter to the listview.
myListView.setAdapter(aa);

myEditText.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
      if (event.getAction() == KeyEvent.ACTION_DOWN)
        if (keyCode == KeyEvent.KEYCODE_ENTER)
        {
          todoItems.add(0, myEditText.getText().toString());
          aa.notifyDataSetChanged();
          myEditText.setText("");
          return true;
        }
      return false;
    }
  });
  }
}

activity_main.xml中

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <EditText
        android:id="@+id/myEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="New To Do Item"
    />

    <ListView
        android:id="@+id/myListView"
        android:layout_width="match_parent"
        android:layout_height="427dp" >
    </ListView>

推荐答案

首先检查myEditText.setOnKeyListener是否被解雇时,pressing Enter键。

first check whether myEditText.setOnKeyListener is getting fired when you pressing Enter.

如果是的话那么在你的EditText XML添加这些属性。

If yes then add these properties in your edittext xml

ndroid:singleLine="true"
android:lines="1"

希望它会工作。

这篇关于待办事项例如我不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆