我可以在onCreate()中调用另一个类的值吗?
问题描述:
我想弄清楚是否有方法来调用“submitValue”,在createNewSubmitButton();在onCreate();功能?我可以在onCreate()中调用另一个类的值吗?
所以我想提交EditText中的值到一个TextView当你点击Button提交值,所以我在那里设置了一个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;
}
}
}
我希望有人能帮助我
答
事实上,你不能调用尚未在您的视图中创建的财产以后。
你仍然有2个选项:
A - 在您的onCreate(...)
创建,但稍后将其添加到您的视图
B-创建它,添加它,但使用如下因素参数:
sibmitButton.setVisibility(View.GONE);
这件事情很酷,你可以添加按钮,玩它(编程),但用户无法触摸它!它甚至不会在你的视野中占据任何“空间”!
并在需要时,只需使用:
sibmutButton.setVisibility(View.VISIBLE);
难道你想要什么?
+0
是啊...这实际上是我想要的... 我一直在想这一切都是错的。 但现在它正在工作......谢谢! – 2012-02-17 19:17:33
你是什么意思“call submitValue”? submitValue是createNewSubmitButton方法中的局部变量。你当然可以调用createNewSubmitButton。除此之外,我对你想要做的事情感到非常困惑。你能澄清吗? – LuxuryMode 2012-02-16 20:00:01