gpt4 book ai didi

android - 减少 Button onClick 事件的代码

转载 作者:太空宇宙 更新时间:2023-11-03 10:30:08 25 4
gpt4 key购买 nike

我的问题分为三个部分。

我的布局包含 26 个按钮:ButtonA、ButtonB、ButtonC...ButtonZ

按钮在屏幕上按顺序排列。单击按钮时,我想捕获单击事件并按单击的第一个字母过滤单词的 SQLiteDB。

如何编写最少量的代码来捕获点击、识别按钮的对应字母并从 SQLite 数据库中返回以所选字母开头的字母?

以下是我的代码,尚未针对代码简洁性进行优化。

//Create your buttons and set their onClickListener to "this"
Button b1 = (Button) findViewById(R.id.Button04);
b1.setOnClickListener(this);

Button b2 = (Button) findViewById(R.id.Button03);
b2.setOnClickListener(this);


//implement the onClick method here
public void onClick(View v) {
// Perform action on click
switch(v.getId()) {
case R.id.Button04:
//Toast.makeText(this,"test a",Toast.LENGTH_LONG).show();

// Do the SqlSelect and Listview statement for words that begin with "A" or "a" here.

break;
case R.id.Button03:
//Toast.makeText(this,"test b",Toast.LENGTH_LONG).show();

// Do the SqlSelect and Listview statement for words that begin with "A" or "a" here.
break;
}

最佳答案

如果你有很多字符和每个字符的按钮,我可能会扩展“BaseAdapter”类并制作一个 ButtonAdapter 将字母表作为字符数组保存,而不是实际制作 28:ish 按钮...... .如果我正确理解了问题?

getView 可能看起来像这样:

  public View getView(int position, View convertView, ViewGroup parent) {
Button button;

if (convertView == null) {
button = new Button(mContext);
button.setTag(alphabet[position]);
button.setOnClickListener(clickListener);
} else {
button = (Button) convertView;
}

button.setText(alphabet[position]);
return button;
}

关于android - 减少 Button onClick 事件的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7134996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com