如何为按钮分配方法?

发布于 2024-09-25 01:52:10 字数 2300 浏览 0 评论 0原文

这个问题看起来很愚蠢,但我花了很多时间试图成功解决它。问题是,如果我尝试在普通 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱自由 2024-10-02 01:52:10

尝试以下操作。当信号强度发生变化时,PhoneStateListener 只是将信号强度写入局部变量 signalDBM。并且您的活动实现了 OnClickListener 的方法 onClick()。在这里,您读取 signalDBM 的值并将其传递给 toast。

package de.test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class analyzer extends Activity implements OnClickListener{ 
    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);

             GetParams listener = new GetParams();
             TelephonyManager TelManager = ( TelephonyManager )getSystemService(Context.TELEPHONY_SERVICE);
             TelManager.listen(listener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

             Button btn=(Button)findViewById(R.id.start);
             btn.setOnClickListener(this);             
         }


            @Override
         public void onClick(View v) {
             switch (v.getId()) {
                    case R.id.start:
                         Toast.makeText(this, "GSM signal strength is " + this.signalDBM , Toast.LENGTH_SHORT).show();
                         break;                    
            }
        }        
}

Manifest.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="de.test"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".analyzer"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
</manifest> 

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.

package de.test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class analyzer extends Activity implements OnClickListener{ 
    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);

             GetParams listener = new GetParams();
             TelephonyManager TelManager = ( TelephonyManager )getSystemService(Context.TELEPHONY_SERVICE);
             TelManager.listen(listener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

             Button btn=(Button)findViewById(R.id.start);
             btn.setOnClickListener(this);             
         }


            @Override
         public void onClick(View v) {
             switch (v.getId()) {
                    case R.id.start:
                         Toast.makeText(this, "GSM signal strength is " + this.signalDBM , Toast.LENGTH_SHORT).show();
                         break;                    
            }
        }        
}

The Manifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="de.test"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".analyzer"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
</manifest> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文