Android:onCreate() 中的侦听器模式

发布于 2024-11-09 12:31:02 字数 1647 浏览 0 评论 0原文

所以,我再次问一个非常基本的问题。我为我的无能道歉,但我想我对这些主题的教程读得不好。我的问题如下:

我想使用“侦听器”模式来处理 GUI 上的按钮按下操作。我相信 onClickListener 是我需要用来处理这些按钮按下的东西。但是,我不确定是否应该在 onCreate 方法中创建和处理 GUI 创建后发生的事件。以下代码位于我的活动之一的 onCreate 方法中:

View.OnClickListener upDownListener = new View.OnClickListener() 
    {       
        @Override
        public void onClick(View v) 
        {


            if(v == (upOneButton))
            {
                Log.d("OptionSelect", "Up One Button Pressed.");
                ops.getOptionList().get(0).incrementProbability(4);
            } . . .

被调用的该方法用不同的编号更新一些 GUI 文本。正在调用它,但 GUI 没有响应。我想这与我尝试在 onCreate 方法中使用它有关。

简而言之,在 GUI 中处理用户事件的好且简单的方法是什么?应该在哪里进行?

太感谢了。

编辑:Log.d() 实际上被调用。另外,ops 是一个 OptionSelect 类型的对象,它恰好是调用 onCreate() 的类的类型。这会成为一个问题吗?另外,这里是incrementProbability()的方法:

public void incrementProbability(int numberOfOptions)
    {
        probability += (numberOfOptions - 1);
        if(probability > 100)
        {
            Log.i("OptionSelect", "Exceeded Maximum by " + (probability - 100));
            probability = 100;
        }
    }

另外,这里是我应该包含的相关代码,它在onClick()方法的末尾更新我的GUI:

    private void refreshDisplay(TextView a, TextView b, TextView c, TextView d)
{
    a.setText(getOptionList().get(0).getProbability() + "");
    b.setText(getOptionList().get(1).getProbability() + "");
    c.setText(getOptionList().get(2).getProbability() + "");
    d.setText(getOptionList().get(3).getProbability() + "");

    a.invalidate();
    b.invalidate();
    c.invalidate();
    d.invalidate();
}

感谢到目前为止的帮助!

So, I am again, asking a very basic question. I apologize for my ineptness but I guess I read the given tutorials on these topics poorly. My question is as follows:

I would like to use a "listener" pattern to handle button presses on my GUI. I believe an onClickListener is what I need to use to handle these button presses. However, I'm not sure if I should be creating and handling events that occur after the GUI is created within an onCreate method. The following code is within my onCreate method for one of my Activities:

View.OnClickListener upDownListener = new View.OnClickListener() 
    {       
        @Override
        public void onClick(View v) 
        {


            if(v == (upOneButton))
            {
                Log.d("OptionSelect", "Up One Button Pressed.");
                ops.getOptionList().get(0).incrementProbability(4);
            } . . .

This method being called updates some GUI text with a different number. It is being called, but the GUI isn't responding. I imagine this has to do with my attempt to use it within the onCreate method.

In short, what is a good and simple way to deal with user events within a GUI and where should this occur?

Thank you so much.

EDIT: Log.d() does in fact get called. Also, ops is an object of type OptionSelect which happens to be the type of the class in which the onCreate() call is made. Will that become an issue? Also, here is the method for incrementProbability():

public void incrementProbability(int numberOfOptions)
    {
        probability += (numberOfOptions - 1);
        if(probability > 100)
        {
            Log.i("OptionSelect", "Exceeded Maximum by " + (probability - 100));
            probability = 100;
        }
    }

Also, here is relevant code I should've included that is updating my GUI at the end of the onClick() method:

    private void refreshDisplay(TextView a, TextView b, TextView c, TextView d)
{
    a.setText(getOptionList().get(0).getProbability() + "");
    b.setText(getOptionList().get(1).getProbability() + "");
    c.setText(getOptionList().get(2).getProbability() + "");
    d.setText(getOptionList().get(3).getProbability() + "");

    a.invalidate();
    b.invalidate();
    c.invalidate();
    d.invalidate();
}

Thanks for the help so far!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与君绝 2024-11-16 12:31:02

我个人更喜欢让我的 Activity 实现侦听器接口,并向 Activity 本身添加一个 onClick 方法,例如...

public class MyActivity extends Activity
    implements View.OnClickListener {

    ...

    @Override
    public void onClick(View v) {

        ...

    }
}

然后我只需使用...

myGuiObject.setOnClickListener(this);

...每当我想将该方法设置为任何 GUI 对象的侦听器时。

I personally prefer to have my Activities implement listener interfaces and add an onClick method to the Activity itself such as...

public class MyActivity extends Activity
    implements View.OnClickListener {

    ...

    @Override
    public void onClick(View v) {

        ...

    }
}

I then just use...

myGuiObject.setOnClickListener(this);

...whenever I want to set that method as the listener for any GUI object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文