gpt4 book ai didi

java - 将 Action 传递给按钮

转载 作者:行者123 更新时间:2023-11-30 04:26:58 25 4
gpt4 key购买 nike

我需要在 Android 中将一个 Intent 传递给 onClickListener..但方式与正常方式略有不同。

我有按钮的 gridView,每次第一次点击每个按钮时,它只会说明该按钮执行的操作(适用于盲人),我需要在用户连续点击同一个按钮两次时启动 Intent。我有这个适配器

public class BlindAdapter extends BaseAdapter implements OnClickListener{
private Context mContext;
private String[] labels;
private Button currentlyClicked;

public BlindAdapter(Context c, String[] labels) {/*Empty*/}

@Override
public int getCount() {/*Empty*/}

@Override
public Object getItem(int arg0) {/*Empty*/}

@Override
public long getItemId(int arg0) {/*Empty*/}

@Override
public View getView(int position, View convertView, ViewGroup parent) {/*Empty*/}

private void say(String text) {/*Empty*/}

@Override
public void onClick(View view) {
Button btn = (Button) view;
Log.v("id", ""+view.hashCode());
if(this.currentlyClicked == null || this.currentlyClicked.hashCode() != view.hashCode()){
if(this.currentlyClicked != null) this.currentlyClicked.setBackgroundColor(Color.LTGRAY);
this.currentlyClicked = (Button) view;

btn.setBackgroundColor(Color.GRAY);
this.say(btn.getText().toString());
} else{
this.say("Zvolena volba:" + btn.getText().toString());
btn.setBackgroundColor(Color.LTGRAY);
this.currentlyClicked = null;
}
}

//Notice I posted inly onCLick method with its body for simplicity..

然后我像那样实例化它

gridview.setAdapter(
new BlindAdapter(this.getApplicationContext(),
new String[]{"jedna", "dva", "tri", "ctyri", "pet", "šest", "osm"})
);

字符串是对用户来说是红色的操作。因此,我什至需要将每个特定按钮的 Intent 传递到那里。是否可以将它作为另一个参数传递到那里?

或者有可能以某种方式覆盖我的 Activity 中的按钮 onclick 事件,并首先调用 super.OnClick 应用程序决定是第一次点击还是第二次点击,根据返回我将停止整个 onclick 事件,还是继续回到 Activity 中的覆盖事件并调用 Intent?

或者有更好的方法来实现这一切吗? (我觉得至少我想到的第一种可能真的很奇怪)。

编辑

那么如何让每个项目都“可聚焦”..它会更易于访问(轨迹球等)。可以让 gridViewItem 只在第一次点击时被选中(只需聚焦)并且只在下一次点击时点击..(这​​是我想要实现的行为)..

最佳答案

我不确定我是否正确理解了问题,但由于您的用例非常具体,您可能会在您的应用程序中再次需要它,我要做的是创建一个自定义按钮并在我的布局中使用它而不是原生的。对于要读取的字符串,我会使用标签属性而不是文本,但这在代码中几乎没有区别。

public class BlindButton extends Button {
private boolean firstTime = true;
private Intent intent;

[...]

public BlindButton(Context context, AttributeSet attrs) {
[...]
setOnClickListener(new OnClickListener() {
@Override
public void onClick() {
if (firstTime) {
firstTime = false;
say(getText()); // or getTag()
} else {
if (intent != null) {
getContext().startActivity(intent);
}
}
}
});
}

private void setIntent(Intent intent) {
this.intent = intent;
}
private void say(String text) {
[...]
}
}

对于 Intent ,我认为这取决于你想要触发什么样的 Action 。我认为上面的 setIntent 可以工作,虽然我不太喜欢它,但是没有更多信息我想不出更优雅的解决方案。基本上,在您的适配器中使用 BlindButton 时,您不必编写任何额外的代码(例如 setOnClickListener 或任何其他代码),而只需对其调用 setIntent。

关于java - 将 Action 传递给按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8322629/

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