如何为按钮分配方法?
这个问题看起来很愚蠢,但我花了很多时间试图成功解决它。问题是,如果我尝试在普通 Java 中使用的任何解决方案,Eclipse IDE 会抛出各种不同的错误。另一件事是,在实现接口后,我收到一个错误,并且 Eclipse 将我的类更改为抽象类。所以源代码是:
public class analyzer extends Activity {
TextView dateAndTimeLabel;
private Button closeButton;
private int signalDBm = 0;
public class GetParams extends PhoneStateListener
{
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
super.onSignalStrengthsChanged(signalStrength);
signalDBm = signalStrength.getGsmSignalStrength();
}
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView tv = (TextView)findViewById(R.id.text);
tv.setText("Greetings, My Lord");
EditText fld=(EditText)findViewById(R.id.field);
fld.setText("Nothing here at the moment");
new GetParams();
Button btn=(Button)findViewById(R.id.start);
btn.setOnClickListener(this);
btn=(Button)findViewById(R.id.stop);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
/**updateLabel();*/
/**Zakritie programmi*/
this.closeButton = (Button)this.findViewById(R.id.end);
this.closeButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
/**Prosmotr loga*/
Button btn_log=(Button)findViewById(R.id.viewlog);
btn_log.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView tv = (TextView)findViewById(R.id.text);
tv.setText("U pressed the button, now you will die!");
EditText fld=(EditText)findViewById(R.id.field);
fld.setText("Power: " + signalDBm +" dBm\n" + "BER:... \n" + "Coordinates: ... \n");
};
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
Toast.makeText(this, "GSM signal strength is " + signalDBm, Toast.LENGTH_SHORT).show();
break;
}
}
};
所以有一个我想使用的类 GetParams 及其方法 signalStrength.getGsmSignalStrength() 。我还可以补充的一件事是 SignalStrength() 类没有公共构造函数,这是我访问 getGsmSignalStrength() 方法的唯一方法。如果有人可以帮助我,我会非常高兴,因为在找到解决方案之前我所有的工作都会停止。
The question seems quite stupid, but I spent quite a lot of time trying to solve it successfully. The problem is that Eclipse IDE throws me a various number of different errors if I try any solutions that I used in normal Java. Another thing is that just after implementing an interface I receive an error, and Eclipse change my class to abstract. So the source code is:
public class analyzer extends Activity {
TextView dateAndTimeLabel;
private Button closeButton;
private int signalDBm = 0;
public class GetParams extends PhoneStateListener
{
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
super.onSignalStrengthsChanged(signalStrength);
signalDBm = signalStrength.getGsmSignalStrength();
}
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView tv = (TextView)findViewById(R.id.text);
tv.setText("Greetings, My Lord");
EditText fld=(EditText)findViewById(R.id.field);
fld.setText("Nothing here at the moment");
new GetParams();
Button btn=(Button)findViewById(R.id.start);
btn.setOnClickListener(this);
btn=(Button)findViewById(R.id.stop);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
/**updateLabel();*/
/**Zakritie programmi*/
this.closeButton = (Button)this.findViewById(R.id.end);
this.closeButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
/**Prosmotr loga*/
Button btn_log=(Button)findViewById(R.id.viewlog);
btn_log.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView tv = (TextView)findViewById(R.id.text);
tv.setText("U pressed the button, now you will die!");
EditText fld=(EditText)findViewById(R.id.field);
fld.setText("Power: " + signalDBm +" dBm\n" + "BER:... \n" + "Coordinates: ... \n");
};
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
Toast.makeText(this, "GSM signal strength is " + signalDBm, Toast.LENGTH_SHORT).show();
break;
}
}
};
So there is a class GetParams with its method signalStrength.getGsmSignalStrength() that I want to use. One more thing I can add is that SignalStrength() class does not have a public constructor and this is the only way i can get to the method getGsmSignalStrength(). I would be very glad, if someone could help me because all my work stopped until I find a solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下操作。当信号强度发生变化时,PhoneStateListener 只是将信号强度写入局部变量 signalDBM。并且您的活动实现了 OnClickListener 的方法 onClick()。在这里,您读取 signalDBM 的值并将其传递给 toast。
Manifest.xml 文件:
Try the following. The PhoneStateListener simply writes the signal strength to the local variable signalDBM whenever the signal strength changes. And your activity implements the OnClickListener's method onClick(). Here you read the value of signalDBM and pass it to the toast.
The Manifest.xml file: