我OnClickListener函数内部不能达到我的(最终)按钮 [英] Can't reach my (final) button inside my OnClickListener function

查看:132
本文介绍了我OnClickListener函数内部不能达到我的(最终)按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的这个片段的最终目标是:

My final goal of this snippet is to:


    从按钮
  1. 调用一个对话框(界面)。

  2. 让最终用户选择一个选项(在5选项列表)

  3. 按钮文本更改为所选的选项

目前我有这样的:

public void onCreate(Bundle savedInstanceState) {
   setLayoutState();
   // rest of code omitted
}

那么instatiates该按钮的setLayoutState()

then the setLayoutState() that instatiates the button

public void setLayoutState() {
    setContentView(R.layout.main);
    Button rate = (Button) findViewById(R.id.ratebutton);
    rate.setOnClickListener(onRatePress); 
}

所以在这里: setOnClickListener调用一个单独的函数(保持干净的东西,活动已经得到了很多按钮)

private final View.OnClickListener onRatePress = new View.OnClickListener() {
public void onClick(View v) {

final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                context, R.array.rates, android.R.layout.select_dialog_item );
        adapter.setDropDownViewResource(android.R.layout.select_dialog_item);

        new AlertDialog.Builder(context).setTitle("Rate this item!")
       .setAdapter(adapter, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                Common.makeToast(context,adapter.getItem(which) + "", 3000);
                Button rate = (Button) findViewById(R.id.ratebutton);
                rate.setText(adapter.getItem(which)+"");
                // TODO: user specific action
                dialog.dismiss();
            }
        }).create().show();
    }
};

这工作正常我在想,如果有一种方法可以让我把这事办成的重新声明对话框的onClick里的按钮率

While this works fine I was wondering if there is a way I can pull this off without redeclaring the Button rate inside the Dialog's onClick

我已经尝试宣告按钮为final的顶部,但不会让我打电话,在对话框的onClick按钮。

I've already tried declaring the button as final in the top part, but that won't let me call the button in the Dialog's onClick.

推荐答案

在Java中的变量的作用域。它始终是可用的块(一对大括号{}中),包含其声明,并且在该块中包含的任何块内。但不是ouside。

A variable in Java has a scope. It is always available inside the block (a pair of braces {} ) that contains its declaration, and in any block contained by this block. But not ouside.

因此​​,如果声明一个方法内部的按钮,它不是这个方法以外访问。
您按钮仅内部drupappSetUploadLayout访问。

Thus if you declare your button inside a method, it is not accessible outside of this method. You button is only accessible inside drupappSetUploadLayout.

如果你希望它是所有方法访问,然后把它的类体内直接。这样的变量称为字段,字段是所有方法访问的。

if you want it to be accessible to all methods, then put it inside the class body directly. Such a variable is called a field and fields are accessible to all methods.

public class A {
  private Button b;

  public void foo() {
    b=null;
  }
}

B可以用所有的方法进行访问。

b can be accessed by all methods.

了解更多关于Java的基础知识,你应该考虑与Android之前开始制作小J2SDK程序。

Read more about the basics of java, you should consider making small J2SDK programs before starting with android.

这篇关于我OnClickListener函数内部不能达到我的(最终)按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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