删除onSubmit()方法

时间:2018-10-17 18:50:03

标签: java android android-activity

我在Internet上找到了以下代码,我想知道如何删除onSubmit方法,该方法是单击按钮,并且代码仍然有效。当我简单地删除该方法时,代码就被破坏了。

public class CellSignalStrength extends Activity{
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.info);
    }
    public void onSubmit(View v) {
        TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        int phoneType=tm.getPhoneType();
        String ptype="";
        switch(phoneType) {
            case TelephonyManager.PHONE_TYPE_CDMA:
                ptype="\nPhone Type: CDMA\n";
                break;
            case TelephonyManager.PHONE_TYPE_GSM:
                ptype="\nPhone Type: GSM\n";
                break;
            case TelephonyManager.PHONE_TYPE_SIP:
                ptype="\nPhone Type: SIP\n";
                break;
            case TelephonyManager.PHONE_TYPE_NONE:
                ptype="\nPhone Type: NONE\n";
                break;
        }
        tv.setText(ptype);
    }
}

1 个答案:

答案 0 :(得分:0)

如果您想执行onClick不需要的操作,只需将代码移入

  

onCreate

像这样

public class CellSignalStrength extends Activity{
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.info);
           TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        int phoneType=tm.getPhoneType();
        String ptype="";
        switch(phoneType) {
            case TelephonyManager.PHONE_TYPE_CDMA:
                ptype="\nPhone Type: CDMA\n";
                break;
            case TelephonyManager.PHONE_TYPE_GSM:
                ptype="\nPhone Type: GSM\n";
                break;
            case TelephonyManager.PHONE_TYPE_SIP:
                ptype="\nPhone Type: SIP\n";
                break;
            case TelephonyManager.PHONE_TYPE_NONE:
                ptype="\nPhone Type: NONE\n";
                break;
        }
        tv.setText(ptype);
    }
}