本文介绍了公共无效的onClick外通价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我确定这可能是一个相当简单的问题,但我难倒,因为我是一个初学者。

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

我期待从一个类值传递给另一个,我有我的助手功能下降和工作就好了。如果我我的onClick之外创建一个整数,我可以通过它没有问题。如果我onClick的中创建它,虽然它似乎没有发挥出来。

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 ####
}
}

这帮助似乎并不能够看到所创建的onClick的setHoles值。

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

有什么建议?在此先感谢!

Any suggestions? Thanks in advance!

推荐答案

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

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.

结果
然后让我来阐述。您可以通过键入一个函数外部以下行,在类中创建一个字段:


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

[进入] [类型] [名称];

[Access][Type][Name];

例如:

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


我复制你的code和运行它。 (修改只是一点点。)

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

和它似乎工作得很好。

这篇关于公共无效的onClick外通价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:52