我想知道如何将变量从private void传递给OnCreate?
public final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
switch (msg.arg1) {
case BluetoothChatService.STATE_CONNECTED:
setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
mConversationArrayAdapter.clear();
break;
case BluetoothChatService.STATE_CONNECTING:
setStatus(R.string.title_connecting);
break;
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
setStatus(R.string.title_not_connected);
break;
}
break;
case MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
mConversationArrayAdapter.add("Me: " + writeMessage);
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
int k=Integer.parseInt(readMessage.replaceAll("[\\D]",""));
mConversationArrayAdapter.add(readMessage);
Toast.makeText(getApplicationContext(),"ciao"+k ,Toast.LENGTH_SHORT).show();
break;
case MESSAGE_DEVICE_NAME:
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
Toast.makeText(getApplicationContext(), "Connected to "
+ mConnectedDeviceName, Toast.LENGTH_SHORT).show();
break;
case MESSAGE_TOAST:
Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
Toast.LENGTH_SHORT).show();
break;
}
}
};
这是OnCreate:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mvc = (Button) findViewById(R.id.button1);
mvc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
public void run() {
while (progressStatus < 1000) {
Random r = new Random();
int i1 = r.nextInt(1000);
if (i1 > progressStatus){
progressStatus = i1;
}
else {
progressStatus = progressStatus;
}
// Update the progress bar and display the current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
}
});
try {
// Sleep for 200 milliseconds. Just to display the progress slowly
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
});
res = (Button) findViewById(R.id.button2);
res.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
progressBar.setProgress(0);
progressStatus=0;
}
});
if(D) Log.e(TAG, "+++ ON CREATE +++");
// Set up the window layout
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
// Get local Bluetooth adapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// If the adapter is null, then Bluetooth is not supported
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
return;
}
}
我传递变量 k ,是否有方法?或者我必须去公共场所?我不知道该怎么解决。提前谢谢!
答案 0 :(得分:0)
在课程开头添加int k
例如:
...
public class MainActivity extends Activity {
int k;
//onCreate and other methods
}
...
更改
int k = =Integer.parseInt (readMessage.replaceAll.replaceAll("[\\D]",""));
到
k = =Integer.parseInt(readMessage.replaceAll.replaceAll("[\\D]",""));
因为你已经使k成为整数。
然后你可以在班级的任何地方使用k