如何从 oncreate 方法 android 内部调用同一个 class 中的方法?
How to call a method in same class from inside oncreate method android?
您好,我是 android 和 java 的新手,我试图制作一个三按钮菜单,每次单击一个按钮时,其他两个按钮会更改其颜色或单击一个按钮会突出显示以显示它是选中但是我无法在单击时从 onCreate 内部调用方法来执行特定任务。帮忙谢谢`
public class MainActivity extends AppCompatActivity {
int PriceList;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.Coffee);
Button button2 = (Button)findViewById(R.id.Mocha);
Button button3 = (Button)findViewById(R.id.Lattee);
TextView Counter = (TextView)findViewById(R.id.Counters);
mClick(button1,button2,button3)
mClick(button2,button1,button3)
mClick(button3,button2,button1)
Counter.setText(Item()); //Counter is a TextVew, This code doesn't work
}
**/*------------METHODS----------------*/**
public int TotalValue(int param5){
if(param5 == 2131427416){ //just Trying to compare with id value
Item();
Log.d("Item Value " , "onCreate: The value is " + Item()); // works upto here i.e Item()=1
} else{
// some other code here
}
}
public int Item(){
PriceList = 1;
return PriceList;
}
public void mClick(final Button param1,final Button param2,final Button param3){
param1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
param2.setBackgroundColor(Color.rgb(192,110,99));
param3.setBackgroundColor(Color.rgb(192,110,99));
int IdNum = param1.getId()
TotalValue(IdNum);
}
});
}
}
}
您不能在方法中定义方法:
public class Example {
void foo() {
void bar() { }
}
无效!争取:
public class Example {
void foo() {
}
void bar() { }
相反。
除此之外,要了解的另一件基本事情是,为了让多个方法对相同的变量起作用,这些方法需要是 class 的 字段 , 比如:
public class Example {
private Button button1
void onCreate() {
...
button1 = (Button) findViewById(R.id.button1);
...
}
void foo() {
do something with button1
}
旧答案
您的问题是关于 button's initializations
:
您正在呼叫MyMethod(mybutton1, mybutton2, mybutton3);
但您永远不会初始化您的 buttons
,因此它们将永远是 null
。
TextView
也是如此。
在 Android 中你必须像下面这样调用 init:
public void onCreate(...){
Button button1 = (Button)findViewById(R.id.Coffee);
Button button2 = (Button)findViewById(R.id.Mocha);
Button button3 = (Button)findViewById(R.id.Lattee);
TextView Counter = (TextView) findViewById(R.id.Counters);
mClick(button1,button2,button3);
mClick(button2,button1,button3);
mClick(button3,button2,button1);
Counter.setText(Item()); //Counter is a TextVew, This code doesn't work
}
Check for those videos on youtube,它们是 android 初学者的最佳选择 恕我直言。
祝你好运!如有任何帮助,我在这里
改进:
您的代码应该是这样的。请记住格式化代码以便更好地理解:
public class MainActivity extends AppCompatActivity {
private int priceList;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
TextView counter = (TextView)findViewById(R.id.Counter);
mClick(button1,button2,button3)
mClick(button2,button1,button3)
mClick(button3,button2,button1)
counter.setText(Item()); //Dont' use capital letters for variable's name
}
**/*------------METHODS----------------*/**
public int TotalValue(int param5){
if(param5 == 2131427416){ //just Trying to compare with id value
Item();
Log.d("Item Value " , "onCreate: The value is " + Item()); // works upto here i.e Item()=1
} else{
// some other code here
}
}
public int Item(){
priceList = 1;
return priceList;
}
public void mClick(final Button param1,final Button param2,final Button param3){
param1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
param2.setBackgroundColor(Color.rgb(192,110,99));
param3.setBackgroundColor(Color.rgb(192,110,99));
int idNum = param1.getId()
TotalValue(idNum);
}
});
}
}
有几点:
- 最后一个额外的右大括号
- (约定)不要用大写字母调用变量
- 我没有更改您的方法名称和变量名称,但考虑使用更明确的名称(仍然是约定俗成,但这是为了更好地理解)
- 您在方法调用后没有
;
改进更新:
您在 mClick(..)
次通话后错过了 ;
。
根据我对你想要什么的理解,你可以使用这个
public class LoginActivity extends AppCompatActivity {
int PriceList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.Coffee);
final Button button2 = (Button) findViewById(R.id.Mocha);
final Button button3 = (Button) findViewById(R.id.Lattee);
TextView Counter = (TextView) findViewById(R.id.Counters);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button2.setBackgroundColor(Color.rgb(192, 110, 99));
button3.setBackgroundColor(Color.rgb(192, 110, 99));
int IdNum = button1.getId();
TotalValue(IdNum);
}
});
Counter.setText(Item()); //Counter is a TextVew, This code doesn't work
}
public int TotalValue(int param5) {
if (param5 == 2131427416) { //just Trying to compare with id value
Item();
Log.d("Item Value ", "onCreate: The value is " + Item()); // works upto here i.e Item()=1
} else {
// some other code here
}
return 0;
}
public int Item() {
PriceList = 1;
return PriceList;
}
}
如果没有,请评论确切的需要,以便我可以提供帮助
您好,我是 android 和 java 的新手,我试图制作一个三按钮菜单,每次单击一个按钮时,其他两个按钮会更改其颜色或单击一个按钮会突出显示以显示它是选中但是我无法在单击时从 onCreate 内部调用方法来执行特定任务。帮忙谢谢`
public class MainActivity extends AppCompatActivity {
int PriceList;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.Coffee);
Button button2 = (Button)findViewById(R.id.Mocha);
Button button3 = (Button)findViewById(R.id.Lattee);
TextView Counter = (TextView)findViewById(R.id.Counters);
mClick(button1,button2,button3)
mClick(button2,button1,button3)
mClick(button3,button2,button1)
Counter.setText(Item()); //Counter is a TextVew, This code doesn't work
}
**/*------------METHODS----------------*/**
public int TotalValue(int param5){
if(param5 == 2131427416){ //just Trying to compare with id value
Item();
Log.d("Item Value " , "onCreate: The value is " + Item()); // works upto here i.e Item()=1
} else{
// some other code here
}
}
public int Item(){
PriceList = 1;
return PriceList;
}
public void mClick(final Button param1,final Button param2,final Button param3){
param1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
param2.setBackgroundColor(Color.rgb(192,110,99));
param3.setBackgroundColor(Color.rgb(192,110,99));
int IdNum = param1.getId()
TotalValue(IdNum);
}
});
}
}
}
您不能在方法中定义方法:
public class Example {
void foo() {
void bar() { }
}
无效!争取:
public class Example {
void foo() {
}
void bar() { }
相反。
除此之外,要了解的另一件基本事情是,为了让多个方法对相同的变量起作用,这些方法需要是 class 的 字段 , 比如:
public class Example {
private Button button1
void onCreate() {
...
button1 = (Button) findViewById(R.id.button1);
...
}
void foo() {
do something with button1
}
旧答案
您的问题是关于 button's initializations
:
您正在呼叫MyMethod(mybutton1, mybutton2, mybutton3);
但您永远不会初始化您的 buttons
,因此它们将永远是 null
。
TextView
也是如此。
在 Android 中你必须像下面这样调用 init:
public void onCreate(...){
Button button1 = (Button)findViewById(R.id.Coffee);
Button button2 = (Button)findViewById(R.id.Mocha);
Button button3 = (Button)findViewById(R.id.Lattee);
TextView Counter = (TextView) findViewById(R.id.Counters);
mClick(button1,button2,button3);
mClick(button2,button1,button3);
mClick(button3,button2,button1);
Counter.setText(Item()); //Counter is a TextVew, This code doesn't work
}
Check for those videos on youtube,它们是 android 初学者的最佳选择 恕我直言。
祝你好运!如有任何帮助,我在这里
改进:
您的代码应该是这样的。请记住格式化代码以便更好地理解:
public class MainActivity extends AppCompatActivity {
private int priceList;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
TextView counter = (TextView)findViewById(R.id.Counter);
mClick(button1,button2,button3)
mClick(button2,button1,button3)
mClick(button3,button2,button1)
counter.setText(Item()); //Dont' use capital letters for variable's name
}
**/*------------METHODS----------------*/**
public int TotalValue(int param5){
if(param5 == 2131427416){ //just Trying to compare with id value
Item();
Log.d("Item Value " , "onCreate: The value is " + Item()); // works upto here i.e Item()=1
} else{
// some other code here
}
}
public int Item(){
priceList = 1;
return priceList;
}
public void mClick(final Button param1,final Button param2,final Button param3){
param1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
param2.setBackgroundColor(Color.rgb(192,110,99));
param3.setBackgroundColor(Color.rgb(192,110,99));
int idNum = param1.getId()
TotalValue(idNum);
}
});
}
}
有几点:
- 最后一个额外的右大括号
- (约定)不要用大写字母调用变量
- 我没有更改您的方法名称和变量名称,但考虑使用更明确的名称(仍然是约定俗成,但这是为了更好地理解)
- 您在方法调用后没有
;
改进更新:
您在 mClick(..)
次通话后错过了 ;
。
根据我对你想要什么的理解,你可以使用这个
public class LoginActivity extends AppCompatActivity {
int PriceList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.Coffee);
final Button button2 = (Button) findViewById(R.id.Mocha);
final Button button3 = (Button) findViewById(R.id.Lattee);
TextView Counter = (TextView) findViewById(R.id.Counters);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button2.setBackgroundColor(Color.rgb(192, 110, 99));
button3.setBackgroundColor(Color.rgb(192, 110, 99));
int IdNum = button1.getId();
TotalValue(IdNum);
}
});
Counter.setText(Item()); //Counter is a TextVew, This code doesn't work
}
public int TotalValue(int param5) {
if (param5 == 2131427416) { //just Trying to compare with id value
Item();
Log.d("Item Value ", "onCreate: The value is " + Item()); // works upto here i.e Item()=1
} else {
// some other code here
}
return 0;
}
public int Item() {
PriceList = 1;
return PriceList;
}
}
如果没有,请评论确切的需要,以便我可以提供帮助