简体   繁体   English

无法到达我的OnClickListener函数中的(最终)按钮

[英]Can't reach my (final) button inside my OnClickListener function

My final goal of this snippet is to: 我的最终目标是:

  1. call a Dialog(Interface) from a button. 从按钮调用Dialog(Interface)。
  2. let the end user select an option (in a list of 5 options) 让最终用户选择一个选项(在5个选项的列表中)
  3. change the button text to the selected option 将按钮文本更改为所选选项

Currently I have this: 目前我有这个:

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

then the setLayoutState() that instatiates the button 然后setLayoutState()实例化按钮

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

So here: setOnClickListener calls to a separate function (to keep things clean, the Activity has got a lot of buttons) 所以在这里: setOnClickListener调用了一个单独的函数(为了保持环境整洁,Activity有很多按钮)

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();
    }
};

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 虽然这很好用,但我想知道是否有办法在重新声明Dialog onClick内的Button速率的情况下实现这一目标

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. 我已经尝试在顶部将按钮声明为final,但这不会让我在Dialog的onClick中调用该按钮。

A variable in Java has a scope. Java中的变量具有作用域。 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. 但不是欧塞德。

Thus if you declare your button inside a method, it is not accessible outside of this method. 因此,如果在方法内部声明按钮,则无法在该方法外部访问它。 You button is only accessible inside drupappSetUploadLayout. 您只能在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 can be accessed by all methods. b可以用所有方法访问。

Read more about the basics of java, you should consider making small J2SDK programs before starting with android. 阅读有关Java基础的更多信息,您应该在开始使用android之前考虑制作小型J2SDK程序。

作为参数的View v引用被单击的按钮...,因此您可以删除按钮的重新声明并使用

 ( (Button) v).setText(adapter.getItem(which)+"");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM