简体   繁体   中英

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.
  2. let the end user select an option (in a list of 5 options)
  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

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

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 I was wondering if there is a way I can pull this off 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.

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.

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 can be accessed by all methods.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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