在 public void onClick 之外传递值

发布于 2024-11-26 19:55:08 字数 4275 浏览 1 评论 0原文

所以我确信这可能是一个相当简单的问题,但我很困惑,因为我是初学者。

我希望将一个值从一个类传递到另一个类,并且我的辅助函数已经关闭并且工作得很好。如果我在 onClick 之外创建一个整数,我可以毫无问题地传递它。如果我在 onClick 中创建它,但它似乎没有成功。

package com.movi.easypar;

//import java.util.logging.Handler;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class EntryScreen extends Activity implements OnClickListener {

Button buttonSetHoles, buttonSetPlayers, buttonLetsGo;
TextView textGameSetup;
public int setHoles; <--- declared here###############################
private String name1 = "Crista";
private String name2 = "Rob";
private String name3 = "Gato";
private String name4 = "Movi";



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.entryscreen);


    //******************//
    //***DEFINE FONTS***//
    //******************//
    Typeface merge = Typeface.createFromAsset(getAssets(), "merge.otf");

    //*****************************************************//
    //***CREATES THE UI FOR EACH OF THESE IN JAVA MEMORY***//
    //*****************************************************//
    buttonSetHoles = (Button)findViewById(R.id.buttonSetHoles);
    buttonSetPlayers = (Button)findViewById(R.id.buttonSetPlayers);
    buttonLetsGo = (Button)findViewById(R.id.buttonLetsGo);
    textGameSetup = (TextView)findViewById(R.id.textGameSetup);
    buttonSetHoles.setTypeface(merge);
    buttonSetPlayers.setTypeface(merge);
    buttonLetsGo.setTypeface(merge);
    textGameSetup.setTypeface(merge);
    buttonSetHoles.setText("Set Holes");
    buttonLetsGo.setText("Lets Go");
    buttonSetPlayers.setText("Set Players");

    //******************************//
    //***DEFINES BUTTON LISTENERS***//
    //******************************//
    buttonSetHoles.setOnClickListener(this);
    buttonSetPlayers.setOnClickListener(this);
    buttonLetsGo.setOnClickListener(this);
}






//*************************************************//
//***SETS ON CLICK FUNCTIONALITY FOR ALL BUTTONS***//
//*************************************************//
@Override
public void onClick(View src) {

    switch(src.getId()){

    case R.id.buttonSetPlayers:
        break;

    case R.id.buttonSetHoles:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final CharSequence[] items = {"18", "9"};
        builder.setTitle("Set Holes");
        builder.setItems(items, new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                if (items[item].equals("9")){
                    EntryScreen.this.setHoles = 9; <---#### VALUE SET HERE ####

                }
                else if (items[item].equals("18")){
                    EntryScreen.this.setHoles = 18;

                }

                return;
            }
        });
        builder.create().show();

        return;

    case R.id.buttonLetsGo:
        //*********************************//
        //***LAUNCHES ACTUAL APPLICATION***//
        //*********************************//
        TranslateAnimation slide = new TranslateAnimation(0, -500, 0,0 );
        slide.setDuration(1000);   
        slide.setFillAfter(true);
        buttonLetsGo.startAnimation(slide);
        buttonSetPlayers.startAnimation(slide);
        buttonSetHoles.startAnimation(slide);
        Intent myIntent = new Intent(src.getContext(), EasyPar.class);
        startActivityForResult(myIntent, 0);
        break;
    }
    EntryScreen.this.finish();
}   


public String getNames() {
    return name1;
}

public void setNames(String playerName1) {
    name1 = playerName1;
}

public int getHoles() {
    return setHoles;  <---- #### THIS DOES NOT SEE VALUE SET IN ONCLICK ####
}
}

此帮助程序似乎无法看到 onClick 创建的 setHoles 值。

有什么建议吗?提前致谢!

So im sure this is probably a fairly easy question but I am stumped because I am a beginner.

I am looking to pass a value from one class to another, and I have my helper function down and working just fine. If i create an integer outside of my onClick I can pass it no problem. If I create it inside the onClick though it doesn't seem to make it out.

package com.movi.easypar;

//import java.util.logging.Handler;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class EntryScreen extends Activity implements OnClickListener {

Button buttonSetHoles, buttonSetPlayers, buttonLetsGo;
TextView textGameSetup;
public int setHoles; <--- declared here###############################
private String name1 = "Crista";
private String name2 = "Rob";
private String name3 = "Gato";
private String name4 = "Movi";



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.entryscreen);


    //******************//
    //***DEFINE FONTS***//
    //******************//
    Typeface merge = Typeface.createFromAsset(getAssets(), "merge.otf");

    //*****************************************************//
    //***CREATES THE UI FOR EACH OF THESE IN JAVA MEMORY***//
    //*****************************************************//
    buttonSetHoles = (Button)findViewById(R.id.buttonSetHoles);
    buttonSetPlayers = (Button)findViewById(R.id.buttonSetPlayers);
    buttonLetsGo = (Button)findViewById(R.id.buttonLetsGo);
    textGameSetup = (TextView)findViewById(R.id.textGameSetup);
    buttonSetHoles.setTypeface(merge);
    buttonSetPlayers.setTypeface(merge);
    buttonLetsGo.setTypeface(merge);
    textGameSetup.setTypeface(merge);
    buttonSetHoles.setText("Set Holes");
    buttonLetsGo.setText("Lets Go");
    buttonSetPlayers.setText("Set Players");

    //******************************//
    //***DEFINES BUTTON LISTENERS***//
    //******************************//
    buttonSetHoles.setOnClickListener(this);
    buttonSetPlayers.setOnClickListener(this);
    buttonLetsGo.setOnClickListener(this);
}






//*************************************************//
//***SETS ON CLICK FUNCTIONALITY FOR ALL BUTTONS***//
//*************************************************//
@Override
public void onClick(View src) {

    switch(src.getId()){

    case R.id.buttonSetPlayers:
        break;

    case R.id.buttonSetHoles:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final CharSequence[] items = {"18", "9"};
        builder.setTitle("Set Holes");
        builder.setItems(items, new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                if (items[item].equals("9")){
                    EntryScreen.this.setHoles = 9; <---#### VALUE SET HERE ####

                }
                else if (items[item].equals("18")){
                    EntryScreen.this.setHoles = 18;

                }

                return;
            }
        });
        builder.create().show();

        return;

    case R.id.buttonLetsGo:
        //*********************************//
        //***LAUNCHES ACTUAL APPLICATION***//
        //*********************************//
        TranslateAnimation slide = new TranslateAnimation(0, -500, 0,0 );
        slide.setDuration(1000);   
        slide.setFillAfter(true);
        buttonLetsGo.startAnimation(slide);
        buttonSetPlayers.startAnimation(slide);
        buttonSetHoles.startAnimation(slide);
        Intent myIntent = new Intent(src.getContext(), EasyPar.class);
        startActivityForResult(myIntent, 0);
        break;
    }
    EntryScreen.this.finish();
}   


public String getNames() {
    return name1;
}

public void setNames(String playerName1) {
    name1 = playerName1;
}

public int getHoles() {
    return setHoles;  <---- #### THIS DOES NOT SEE VALUE SET IN ONCLICK ####
}
}

This helper does not seem to be able to see the setHoles value that is created onClick.

Any suggestions? Thanks in advance!

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

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

发布评论

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

评论(5

寂寞笑我太脆弱 2024-12-03 19:55:08

这是一个范围的事情。函数中定义的变量具有局部作用域,并且在函数返回时将被销毁。如果你想保留你的价值,你需要一个字段来保存它。

[编辑]
那么请允许我详细说明一下。您可以通过在类内部的函数外部键入以下行来创建字段:

[Access][Type][Name];

例如:

class foo{
    public int dice;
    public void onClick(){
         //now the dice's value is saved throught the lifecycle of the Activity
    }
}

[编辑]
我复制了你的代码并运行了它。 (稍微修改了一下。)

public class Main extends Activity implements OnClickListener {

Button buttonSetHoles, buttonSetPlayers, buttonLetsGo;
TextView textGameSetup;
public int setHoles; //<--- declared here###############################
private String name1 = "Crista";
private String name2 = "Rob";
private String name3 = "Gato";
private String name4 = "Movi";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //******************//
    //***DEFINE FONTS***//
    //******************//
    Typeface merge = Typeface.createFromAsset(getAssets(), "merge.otf");

    //*****************************************************//
    //***CREATES THE UI FOR EACH OF THESE IN JAVA MEMORY***//
    //*****************************************************//
    /*

    buttonSetPlayers = (Button)findViewById(R.id.buttonSetPlayers);
    buttonLetsGo = (Button)findViewById(R.id.buttonLetsGo);
    textGameSetup = (TextView)findViewById(R.id.textGameSetup);
    */
    buttonSetHoles = (Button) findViewById(R.id.buttonSetHoles);
    /*
    buttonSetHoles.setTypeface(merge);
    buttonSetPlayers.setTypeface(merge);
    buttonLetsGo.setTypeface(merge);
    textGameSetup.setTypeface(merge);
    buttonSetHoles.setText("Set Holes");
    buttonLetsGo.setText("Lets Go");
    buttonSetPlayers.setText("Set Players");
    */

    //******************************//
    //***DEFINES BUTTON LISTENERS***//
    //******************************//.
    buttonSetHoles.setOnClickListener(this);
    /*
    buttonSetPlayers.setOnClickListener(this);
    buttonLetsGo.setOnClickListener(this);
    */
}

//*************************************************//
//***SETS ON CLICK FUNCTIONALITY FOR ALL BUTTONS***//
//*************************************************//
@Override
public void onClick(View src) {

    switch (src.getId()) {

        case R.id.buttonSetHoles:
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            final CharSequence[] items = { "18", "9" };
            builder.setTitle("Set Holes");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int item) {
                    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                    if (items[item].equals("9")) {
                        setHoles = 9;// <---#### VALUE SET HERE ####
                        Toast.makeText(Main.this, "getHoles()=" + getHoles(), Toast.LENGTH_SHORT);
                    }
                    else if (items[item].equals("18")) {
                        setHoles = 18;
                        Toast.makeText(Main.this, "getHoles()=" + getHoles(), Toast.LENGTH_SHORT);
                    }

                    return;
                }
            });
            builder.create().show();

            return;

    }
    //finish();
}

public String getNames() {
    return name1;
}

public void setNames(String playerName1) {
    name1 = playerName1;
}

public int getHoles() {
    return setHoles;
}
}

而且看起来效果很好。

It's a scope thing. A variable defined in a function has local scope, and will be destroyed when the function returns. You need a field to hold your value if you wish to retain it.

[EDIT]
Then allow me to elaborate. You can create a field by typing the following line outside a function, inside the class:

[Access][Type][Name];

ex:

class foo{
    public int dice;
    public void onClick(){
         //now the dice's value is saved throught the lifecycle of the Activity
    }
}

[EDIT]
I copied your code and ran it. (Modified just a little.)

public class Main extends Activity implements OnClickListener {

Button buttonSetHoles, buttonSetPlayers, buttonLetsGo;
TextView textGameSetup;
public int setHoles; //<--- declared here###############################
private String name1 = "Crista";
private String name2 = "Rob";
private String name3 = "Gato";
private String name4 = "Movi";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //******************//
    //***DEFINE FONTS***//
    //******************//
    Typeface merge = Typeface.createFromAsset(getAssets(), "merge.otf");

    //*****************************************************//
    //***CREATES THE UI FOR EACH OF THESE IN JAVA MEMORY***//
    //*****************************************************//
    /*

    buttonSetPlayers = (Button)findViewById(R.id.buttonSetPlayers);
    buttonLetsGo = (Button)findViewById(R.id.buttonLetsGo);
    textGameSetup = (TextView)findViewById(R.id.textGameSetup);
    */
    buttonSetHoles = (Button) findViewById(R.id.buttonSetHoles);
    /*
    buttonSetHoles.setTypeface(merge);
    buttonSetPlayers.setTypeface(merge);
    buttonLetsGo.setTypeface(merge);
    textGameSetup.setTypeface(merge);
    buttonSetHoles.setText("Set Holes");
    buttonLetsGo.setText("Lets Go");
    buttonSetPlayers.setText("Set Players");
    */

    //******************************//
    //***DEFINES BUTTON LISTENERS***//
    //******************************//.
    buttonSetHoles.setOnClickListener(this);
    /*
    buttonSetPlayers.setOnClickListener(this);
    buttonLetsGo.setOnClickListener(this);
    */
}

//*************************************************//
//***SETS ON CLICK FUNCTIONALITY FOR ALL BUTTONS***//
//*************************************************//
@Override
public void onClick(View src) {

    switch (src.getId()) {

        case R.id.buttonSetHoles:
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            final CharSequence[] items = { "18", "9" };
            builder.setTitle("Set Holes");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int item) {
                    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                    if (items[item].equals("9")) {
                        setHoles = 9;// <---#### VALUE SET HERE ####
                        Toast.makeText(Main.this, "getHoles()=" + getHoles(), Toast.LENGTH_SHORT);
                    }
                    else if (items[item].equals("18")) {
                        setHoles = 18;
                        Toast.makeText(Main.this, "getHoles()=" + getHoles(), Toast.LENGTH_SHORT);
                    }

                    return;
                }
            });
            builder.create().show();

            return;

    }
    //finish();
}

public String getNames() {
    return name1;
}

public void setNames(String playerName1) {
    name1 = playerName1;
}

public int getHoles() {
    return setHoles;
}
}

And it seems to work just fine.

百合的盛世恋 2024-12-03 19:55:08

如果你在方法内部声明变量,外部方法肯定看不到它,它不在同一个范围内,你仍然可以在外部声明它,然后从 onClick() 内部设置一个值 方法。

If you declare the variable inside the method, an external method is surely not able to see it, it's not in the same scope, you can still declare it outside and then set a value from inside the onClick() method.

在巴黎塔顶看东京樱花 2024-12-03 19:55:08

将其声明为方法外部的 public/private 变量。

Declare it as public/private variable outside the methods.

┾廆蒐ゝ 2024-12-03 19:55:08

你确定你的 setHoles 已经被设置了吗?到9还是18?尝试在 onclick 中添加 println(setHoles) 以确保该值设置正确。另外,您在 onCreate 之外声明 setHoles 变量,但与 getHoles() 和 onClick() 在同一类中,对吗?

are you sure your setHoles is even being set? to 9 or 18? try adding a println(setHoles) in your onclick to ensure that the value is being set properly. Also, you are declaring your setHoles variable outside of onCreate but within the same class as getHoles() and onClick() right?

守不住的情 2024-12-03 19:55:08

当您比较字符串时,始终使用equal方法。
比如:

  if (items[item].equals( "9")){
}

我更喜欢在变量上使用 SettersGetters

setHoles(int value){}

以及

int getHoles(){}

when you compare Strings always use equal method.
like:

  if (items[item].equals( "9")){
}

and i prefer to user Setters and Getters on variables:

setHoles(int value){}

and

int getHoles(){}

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