我有 2 个类,用于选择宠物,称为 SelectPetsScreen,其中有一个名为更改号码的按钮。此按钮允许用户更改要发送到新号码(设备)的消息的号码,但我的问题是我试图制作number 的新值是 textedit 中写入的值
number=input.toString();
但是说数字无法解析为变量(但我在其他类中将其设置为公共)下一次尝试是否将其包含在代码中并且仍然具有相同的数字这次尝试我创建了一个调用 setNumber() 方法的对象仍然没有希望: (
SelectPetsScreen.java 包 com.set.petshome;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.*;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.Editable;
import android.view.*;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
public class SelectPetsScreen extends Activity {
fishtank ii=new fishtank();
ImageButton fButton,sButton,tButton;
private MediaPlayer mp0,mp1;
String value;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectscreen);
//menu startup sound Code
mp0=MediaPlayer.create(this,R.raw.menu);
mp0.start();
//End Menu Sound Code
//Change No. Button
Button cn = (Button) findViewById(R.id.cn);
cn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
AlertDialog.Builder alert = new AlertDialog.Builder(SelectPetsScreen.this);
/*Voice
final MediaPlayer mp1=MediaPlayer.create(getBaseContext(),R.raw.menu);
mp1.start();
//End Voice */
//Test
alert.setTitle("Change Number:");
//alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(SelectPetsScreen.this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
value = input.getText().toString();
fishtank a=new fishtank();
a.setNumber(value);
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
// Canceled.
}
});
//End Test
alert.show();
}
});
//End Change No. Button
//Button Fishtank Listener Start
fButton = (ImageButton) findViewById(R.id.imageButton1);
//Listening to button event
fButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Voice
mp1=MediaPlayer.create(getBaseContext(),R.raw.menu);
mp1.start();
//End Voice
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(), fishtank.class);
startActivity(nextScreen);
}
});
//Button Fishtank Listener End
//Button Cats Listener Start
sButton = (ImageButton) findViewById(R.id.imageButton2);
//Listening to button event
sButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Voice
final MediaPlayer mp1=MediaPlayer.create(getBaseContext(),R.raw.menu);
mp1.start();
//End Voice
//Starting a new Intent
Intent nextScreen2 = new Intent(getApplicationContext(), Csoon.class);
startActivity(nextScreen2);
}
});
//Button Cats Listener End
//Button Dogs Listener Start
tButton = (ImageButton) findViewById(R.id.imageButton3);
//Listening to button event
tButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Voice
final MediaPlayer mp1=MediaPlayer.create(getBaseContext(),R.raw.menu);
mp1.start();
//End Voice
//Starting a new Intent
Intent nextScreen3 = new Intent(getApplicationContext(), Dsoon.class);
startActivity(nextScreen3);
}
});
//Button Dogs Listener End
}
}
这是另一个包含方法 setNumber() 和 number 变量的类
package com.set.petshome;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
public class fishtank extends Activity {
ToggleButton toggle1,toggle2,toggle3;
public static Bundle bundle = new Bundle();
public String number = "+12020000000";// recipient's mobile number
public void setNumber(String a)
{
number=a.toString();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ftank);
final String[] messages = new String[]{"Feeder is On", "Feeder is OFF","LED is ON","LED is OFF","Filter is ON","Filter is OFF"};
final SmsManager sm = SmsManager.getDefault();
final MediaPlayer on,off;
//menu startup sound Code
on=MediaPlayer.create(this,R.raw.on);
off=MediaPlayer.create(this,R.raw.off);
//End Menu Sound Code
//Start toggleButton1
toggle1 = (ToggleButton) findViewById(R.id.toggleButton1);
toggle1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
on.start();
sm.sendTextMessage(number, null, messages[0], null, null);
} else {
off.start();
sm.sendTextMessage(number, null, messages[1], null, null);
}
}
});
//End toggleButton1
//Start toggleButton2
toggle2 = (ToggleButton) findViewById(R.id.toggleButton2);
toggle2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
on.start();
sm.sendTextMessage(number, null, messages[2], null, null);
} else {
off.start();
sm.sendTextMessage(number, null, messages[3], null, null);
}
}
});
//End toggleButton2
//Start toggleButton3
toggle3 = (ToggleButton) findViewById(R.id.toggleButton3);
toggle3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
on.start();
sm.sendTextMessage(number, null, messages[4], null, null);
} else {
off.start();
sm.sendTextMessage(number, null, messages[5], null, null);
}
}
});
//End toggleButton3
}
//Save Toggle State
@Override
public void onPause() {
super.onPause();
bundle.putBoolean("ToggleButtonState", toggle1.isChecked());
bundle.putBoolean("ToggleButtonState2", toggle2.isChecked());
bundle.putBoolean("ToggleButtonState3", toggle3.isChecked());
}
@Override
public void onResume() {
super.onResume();
toggle1.setChecked(bundle.getBoolean("ToggleButtonState"));
toggle2.setChecked(bundle.getBoolean("ToggleButtonState2"));
toggle3.setChecked(bundle.getBoolean("ToggleButtonState3"));
}
//End Save Toggle State
}