如何访问块外的变量?

时间:2014-02-22 04:18:10

标签: java android

在这个程序中,我有一个TextView,上面写着“输入LineData的数量:”。 在这下面我有一个EditText,它只允许数字。 在下面我有一个Button,其功能是显示EditText的值。 要从EditText获取整数值,我使用了Try/Catch块内的变量“i”。 我知道这个变量“i”在该块之外被销毁。 但我想使用该变量在TableRow中创建TableLayout。 我尝试在Try块中执行此操作。但是我收到了错误。 我怎么能解决这个问题?

public class Ybus_Activity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ybus);
        final LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        final LinearLayout main = (LinearLayout) findViewById(R.id.android_main_layout);
        TextView getData = new TextView(this);
        getData.setText("Enter the number of LineData : ");
        getData.setId(5);
        getData.setLayoutParams(params);
        main.addView(getData);
        final EditText edText = new EditText(this);
        edText.setId(3);
        edText.setLayoutParams(params);
        edText.setWidth(100);
        edText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
        edText.setInputType(InputType.TYPE_CLASS_NUMBER);
        edText.setKeyListener(DigitsKeyListener.getInstance());
        edText.setMaxLines(1);
        main.addView(edText);
        Button bt = new Button(this);
        bt.setText("Click to enter Linedata");
        bt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        main.addView(bt);
        final TextView text = new TextView(this);
        bt.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String ed = edText.getText().toString();
                int i = 0;
                try {
                    i = Integer.parseInt(ed);
                    text.setText(i + "");
                } catch (NumberFormatException ex) {
                    text.setText("Value at TextView is not a valid integer");
                }
            }
        });
        main.addView(text);
    }
}

3 个答案:

答案 0 :(得分:4)

您可以将变量i声明为实例变量

int i;
@Override
public void onCreate(Bundle savedInstanceState) 

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

每次单击按钮时,都会执行按钮单击内的代码。但是,按钮点击外部的代码只执行一次。

因此您没有看到更新的值

编辑:

try {
                i = Integer.parseInt(ed);
                // setting value here
                text.setText(String.valueOf(i));
                // or you can do like this
                // text.setText(String.valueOf(i));
                updatedvalueOfi(i);
            } catch (NumberFormatException ex) {
                text.setText("Value at TextView is not a valid integer");
            }

然后

public void updatedvalueOfi(int value)
{
    Log.i("..........",""+i);
}

编辑:

public class MainActivity extends Activity {
     int i;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        final LinearLayout main = (LinearLayout) findViewById(R.id.ll);
        TextView getData = new TextView(this);
        getData.setText("Enter the number of LineData : ");
        getData.setId(5);
        getData.setLayoutParams(params);
        main.addView(getData);
        final EditText edText = new EditText(this);
        edText.setId(3);
        edText.setLayoutParams(params);
        edText.setWidth(100);
        edText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
        edText.setInputType(InputType.TYPE_CLASS_NUMBER);
        edText.setKeyListener(DigitsKeyListener.getInstance());
        edText.setMaxLines(1);
        main.addView(edText);
        Button bt = new Button(this);
        bt.setText("Click to enter Linedata");
        bt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        main.addView(bt);
        final TextView text = new TextView(this);
        bt.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String ed = edText.getText().toString();

                try {
                    i = Integer.parseInt(ed);
                    // setting value here
                    text.setText(String.valueOf(i));
                    // or you can do like this
                    // text.setText(String.valueOf(i));

                } catch (NumberFormatException ex) {
                    text.setText("Value at TextView is not a valid integer");
                }
            }
        });
        main.addView(text);
        final TextView text2 = new TextView(this);
        Button two = new Button(this);
        two.setText("Second");
        main.addView(two);
        main.addView(text2);
        two.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                text2.setText(String.valueOf(i));

            }

        });

    }

}

对齐

enter image description here

答案 1 :(得分:2)

将变量i声明为静态

public class Ybus_Activity extends Activity {
//Here Declare the vaiable
 static int i=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
}
}

答案 2 :(得分:0)

你的任务的答案。太容易了...... 试着把你的参数i放在按钮的onclicke之外...... 喜欢:

public class Ybus_Activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ybus);
....
....
...
....
int i = 0;    
bt.setOnClickListener(new View.OnClickListener() 
{ 
    public void onClick(View v) 
    {
        String ed=edText.getText().toString(); 

        try{
          i =Integer.parseInt(ed);
          text.setText(i+"");
        }catch(NumberFormatException ex){
          text.setText("Value at TextView is not a valid integer");
        }
    }
});
...
...
...
}

我希望它对你有所帮助:)。