重写了一个dialog窗口,并在上面布置了一些图片。想通过输入数字(按数字键)并调用显示相应的图片,但是总是不成功。
下面是要实现的效果图
输入数字前:输入数字后(例如输入1):这是代码:
private void InputPassword() {
final ImageView passLightImage;

LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.input_password_layout, (ViewGroup) findViewById(R.id.input_password_dialog_id));

final CustomDialog passwordDialog = new CustomDialog(this);

Window dialogWindow = passwordDialog.getWindow();
dialogWindow.setGravity(Gravity.CENTER_VERTICAL );
LayoutParams params = new LayoutParams(300, 150);
passwordDialog.setCanceledOnTouchOutside(false);

passwordDialog.show();
passwordDialog.addContentView(dialogLayout, params);

passLightImage = (ImageView) findViewById(R.id.pass_light_id);

passwordDialog.setOnKeyListener(new OnKeyListener() {

public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN){
switch (keyCode) {
case KeyEvent.KEYCODE_1:
//  passLightImage.setVisibility(View.VISIBLE);
//  passLightImage.setAlpha(0);
 passLightImage.getBackground().setAlpha(0);
//  passLightImage.setVisibility(View.GONE);
 passLightImage.setImageResource(R.drawable.pass_light);
System.out.println("It's 1");
// break;
return true; case KeyEvent.KEYCODE_2:
System.out.println("It's 2");
break; case KeyEvent.KEYCODE_7:
System.out.println("It's 3");
break; case KeyEvent.KEYCODE_6:
System.out.println("It's 4");
break; default:
break;
}
}
return false;
}
});
}

解决方案 »

  1.   

    呵呵,我解决了,改成passLightImage = (ImageView) dialogLayout.findViewById(R.id.pass_light_id),
    谢谢你啦!不过,怎么把软件的所有设置状态都保存起来??
      

  2.   

    试试SharedPreferences,这是api文档中的一个例子
    public class Calc extends Activity {    public static final String PREFS_NAME = "MyPrefsFile";    @Override    protected void onCreate(Bundle state){       super.onCreate(state);       . . .       // Restore preferences       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);       boolean silent = settings.getBoolean("silentMode", false);       setSilent(silent);    }    @Override    protected void onStop(){       super.onStop();      // We need an Editor object to make preference changes.      // All objects are from android.context.Context      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);      SharedPreferences.Editor editor = settings.edit();      editor.putBoolean("silentMode", mSilentMode);      // Commit the edits!      editor.commit();    }}
      

  3.   

    public class Calc extends Activity {
        public static final String PREFS_NAME = "MyPrefsFile";
        @Override
        protected void onCreate(Bundle state){
           super.onCreate(state);
           . . .
           // Restore preferences
           SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
           boolean silent = settings.getBoolean("silentMode", false);
           setSilent(silent);
        }
        @Override
        protected void onStop(){
           super.onStop();
          // We need an Editor object to make preference changes.
          // All objects are from android.context.Context
          SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
          SharedPreferences.Editor editor = settings.edit();
          editor.putBoolean("silentMode", mSilentMode);
          // Commit the edits!
          editor.commit();
        }
    }
      

  4.   

    如果每个界面都有配置状态要保存,是不是每个activity里面都要这样使用?