【问题标题】:Can I call a value of another class in onCreate()?我可以在 onCreate() 中调用另一个类的值吗?
【发布时间】:2012-02-16 19:53:50
【问题描述】:

我想弄清楚是否有一种方法可以调用 createNewSubmitButton(); 中的“submitValue”。在 onCreate();功能?

所以我想在点击按钮提交值时将 EditText 中的值提交到 TextView,所以我在其中设置了一个 onClick,但我无法在 onCreate 中调用 submitValue?

查看我的代码示例以了解我的意思:

package com.lars.myApp;

import com.lars.myApp.R;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;

public class myAppActivity extends Activity {

    int currentValue =  0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final EditText valueOne = (EditText) findViewById(R.id.valueOne);
        Button addOne = (Button) findViewById(R.id.addOne);
        Button minusOne = (Button) findViewById(R.id.minusOne);

        Button addButton = (Button) findViewById(R.id.add);
        final TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1);

        TextView textView = new TextView(this);
        textView.setText("New text");

        addOne.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                addValue();
                valueOne.setText("" + currentValue);
            }
        });

        minusOne.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                minusValue();
                valueOne.setText("" + currentValue);
            }
        });

        addButton.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                tableLayout1.addView(createNewRow());
            }
        });


        // I OUTCOMMENTED THIS, BECAUSE IT DIDN'T WORK, submitValue: "CANNOT BE RESOLVED"

        /*submitValue.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                tableLayout1.addView(createNewTextView(newValue.getText().toString()));
            }

        });*/

    }

    public TableRow createNewRow() {
        final TableRow newRow = new TableRow(this);
        newRow.addView(createNewContainer());
        return newRow;
    }

    public LinearLayout createNewContainer(){
        final LinearLayout newContainer = new LinearLayout(this);
        newContainer.addView(createNewEditableText());
        newContainer.addView(createNewSubmitButton());
        return newContainer;
    }    
    public EditText createNewEditableText() {
        final EditText newValue = new EditText(this);
        newValue.setHint("New");
        return newValue;
    }

    public Button createNewSubmitButton(){
        final Button submitValue = new Button(this);
        submitValue.setText("Submit");
        return submitValue;
    }

    public TextView createNewTextView(String text) {
        final TextView textView = new TextView(this);
        textView.setText(text);
        return textView;
    }

    public void addValue(){
        if(currentValue <= 999){
        currentValue = currentValue + 1;
        }
    }  

    public void minusValue(){
        if(currentValue >= 1){
            currentValue = currentValue - 1;
        }
    }  
}

希望有人能帮助我

【问题讨论】:

  • “调用 submitValue”是什么意思? submitValue 是方法 createNewSubmitButton 中的局部变量。您当然可以调用 createNewSubmitButton。除此之外,我真的很困惑你想要做什么。你能澄清一下吗?

标签: android class view android-activity oncreate


【解决方案1】:

事实上,您不能调用尚未在您的视图中创建的东西。

你还有两个选择:

A - 在您的 onCreate(...) 中创建它,但稍后将其添加到您的视图中

B- 创建它,添加它,但使用以下参数:

sibmitButton.setVisibility(View.GONE);

这个很酷的事情是你可以添加按钮,使用它(以编程方式)但用户不能触摸它!它甚至不会在您的视野中占用任何“空间”!

如果需要,只需使用:

sibmutButton.setVisibility(View.VISIBLE);

这是你想要的吗?

【讨论】:

  • 是的……这实际上就是我想要的……我想这一切都错了。但现在它正在工作......谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-06
  • 1970-01-01
  • 2014-03-05
  • 2010-12-07
相关资源
最近更新 更多