0

我想知道如何EditText使用按钮从ListView. 我试过这段代码:

public class MainActivity extends Activity {
    Button send;
    EditText textBox;

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

        send = (Button) findViewById(R.id.sendBtn);
        textBox = (EditText) findViewById(R.id.textBox);

        EditText text = (EditText) findViewById(R.id.textBox);
        String textBox = text.getText().toString();

        String arr[] = {};

        lv = (ListView) findViewById(R.id.list);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
                                                                R.layout.activity_main, arr);

        lv.setAdapter(adapter);
        textBox.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {}
        });
    }

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

2 回答 2

0
Button send;
EditText textBox;

private ArrayAdapter<String> adapter;

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

    send = (Button) findViewById(R.id.sendBtn);
    textBox = (EditText) findViewById(R.id.textBox);

    EditText text = (EditText) findViewById(R.id.textBox);
    String textBox = text.getText().toString();

    String arr[] = {};

    lv = (ListView) findViewById(R.id.list);

    adapter = new ArrayAdapter<String>(getApplicationContext(),
        R.layout.activity_main, arr);

    lv.setAdapter(adapter);
    send.setOnClickListener(new OnClickListener()
    {

        public void onClick(View arg0)
        {
            adapter.add(textBox.getText().tostring());
            adapter.notifyDataSetChanged(); 
        }

    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
于 2013-07-19T14:19:23.167 回答
0

首先,OnClickListener为 Button 设置一个。在那个监听器中,从EditTextvia中获取文本textBox.getText().toString()。接下来,您要ListView使用新选择的文本更新 's 列表适配器。有很多方法可以做到这一点

  • 更新适配器并通知ListView更改
  • 重建数组并重新Strings设置适配器ListView
于 2013-07-19T14:21:27.863 回答