如何为按钮分配方法?

时间:2010-09-29 10:15:39

标签: java android methods button

这个问题似乎很愚蠢,但我花了很多时间试图成功解决它。问题是如果我尝试在普通Java中使用的任何解决方案,Eclipse IDE会抛出各种不同的错误。另一件事是,在实现接口后,我收到一个错误,Eclipse将我的类更改为abstract。所以源代码是:

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()的唯一方法。我很高兴,如果有人可以帮助我,因为我的所有工作都停止了,直到找到解决方案。

1 个答案:

答案 0 :(得分:1)

尝试以下方法。只要信号强度发生变化,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>