就是我在编写android小程序的时候,经常不是要定义数组嘛,可是数组定义在最上面的类下才有用,如果定义在onCreate方法下就老是报错?请教原因?
代码如下:
package com.yt.email;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Test14Activity extends Activity {
private Button btn;
final int LIIST_DIALOG=1;
boolean[] mul=new boolean[]{true,false,true};//为什么只能定义在这里呢
String[] items=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//不能定义在它下面呢
btn=(Button)findViewById(R.id.mButton);
items=getResources().getStringArray(R.array.msa);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(LIIST_DIALOG);
}
});
}
protected Dialog onCreateDialog(int id){
Dialog dialog= null;
switch (id) {
case LIIST_DIALOG:
AlertDialog.Builder builder= new AlertDialog.Builder(this);
builder.setTitle("提示");
builder.setMultiChoiceItems(R.array.msa, mul,new DialogInterface.
OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
mul[which]=isChecked;
String result="你选择的是:";
for(int i=0;i<mul.length;i++){
if (mul[i]){
result=result+items[i]+"、";
}
}
EditText et=(EditText)findViewById(R.id.myEditText);
et.setText(result);
}
});
dialog=builder.create();
break;
default:
break;
}
return dialog;
}
}
|
|