您好我是Android&的新手java,我试图在每次单击按钮时创建一个三个按钮菜单,其他两个更改其颜色或单击一个突出显示它已选中但我无法调用fromCreate内部的方法来执行特定操作点击任务。帮助谢谢`
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);
}
});
}
}
}
答案 0 :(得分:1)
您无法在方法中定义方法:
public class Example {
private Button button1
void onCreate() {
...
button1 = (Button) findViewById(R.id.button1);
...
}
void foo() {
do something with button1
}
不起作用!寻找:
PrivateIpAddress
代替。
除此之外,要理解的另一个基本要点是,为了让多个方法处理相同的变量,那些需要是类的 fields ,例如:
var params = {
ImageId: 'STRING_VALUE', /* required */
MaxCount: 0, /* required */
MinCount: 0, /* required */
NetworkInterfaces: [
{
PrivateIpAddress: 'STRING_VALUE'
}
/* etc */
};
ec2.runInstances(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
答案 1 :(得分:0)
您的问题与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,对于初学者 imho 来说,它们是最好的。
祝你好运!任何帮助我都在这里这就是你的代码的样子。请记住格式化代码以便更好地理解:
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(..)
。
答案 2 :(得分:0)
从我对你想要的东西的理解,你可以使用这个
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;
}
}
如果没有,请评论确切需要,以便我可以提供帮助