如何分配给按钮的方法? [英] How to assign a method to the button?

查看:110
本文介绍了如何分配给按钮的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题似乎很愚蠢,但我花了很多时间试图成功地解决它。问题是,Eclipse IDE中抛出了我不同的许多不同的错误,如果我尝试,我在普通Java中使用的任何解决方案。另一件事是,仅仅实现接口后,我收到一个错误,而Eclipse改变我的类抽象。因此源$ C ​​$ c是:

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;
        }
}

};

因此​​,有,我想用一个类GetParams其方法signalStrength.getGsmSignalStrength()。还有一件事我可以补充的是,SignalStrength()类没有公共构造函数,这是唯一的方法我可以拿到的方法getGsmSignalStrength()。我将非常高兴,如果有人可以帮助我,因为我所有的工作停止,直到我找到一个解决方案。

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.

推荐答案

请尝试以下方法。该PhoneStateListener只是将信号强度时信号强度改变局部变量signalDBM。和你的活动实现OnClickListener的方法的onClick()。在这里,你读signalDBM的值,并将其传递给敬酒。

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;                    
            }
        }        
}

Manifest.xml文件:

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> 

这篇关于如何分配给按钮的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆