我是 Android 开发的新手,我阻止了一点。我已经执行了一个 Android 教程,一切都很好,但我想ImageView
在我的第二个Activity
.
这是我的第一个Activity
hotel.java
:
public class hotel extends Activity implements View.OnClickListener
{
final String EXTRA_LOGIN = "user_login";
final String EXTRA_PASSWORD = "user_password";
Button button;
int image_index = 0;
private static final int MAX_IMAGE_COUNT=8;
private Integer[]mImageIds ={
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
R.drawable.image5,
R.drawable.image6,
R.drawable.image7,
R.drawable.image8,
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hotel);
ImageView imgView = (ImageView) findViewById(R.id.pictogramme);
imgView.setImageResource(mImageIds[image_index]);
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inScaled = false;
opt.inSampleSize = 1;
opt.inDither = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),mImageIds[image_index] ,opt);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
final byte[]b = baos.toByteArray();
final EditText login = (EditText)findViewById(R.id.user_email);
final EditText pass = (EditText)findViewById(R.id.user_password);
final Button loginButton = (Button) findViewById(R.id.connect);
loginButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(hotel.this, hoteldisplay.class);
intent.putExtra("picture", b);
intent.putExtra(EXTRA_LOGIN, login.getText().toString());
intent.putExtra(EXTRA_PASSWORD,pass.getText().toString());
startActivity(intent);
}
});
Intent intent = getIntent();
Button next = (Button)findViewById(R.id.button_next);
next.setOnClickListener(this);
Button previous = (Button)findViewById(R.id.button_previous);
previous.setOnClickListener(this);
showImage();
}
private void showImage() {
ImageView imgView = (ImageView) findViewById(R.id.pictogramme);
imgView.setImageResource(mImageIds[image_index]);
}
@Override
public void onClick(View v)
{
switch(v.getId())
{
case (R.id.button_previous):
image_index--;
if (image_index == -1){
image_index = MAX_IMAGE_COUNT -1;
}
showImage();
break;
case (R.id.button_next):
image_index++;
if (image_index == MAX_IMAGE_COUNT){
image_index = 0;
}
showImage();
break;
}
}
}
我的第二个Activity
hoteldisplay.java
:
public class hoteldisplay extends Activity {
final String EXTRA_LOGIN = "user_login";
final String EXTRA_PASSWORD = "user_password";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_display);
Intent intent = getIntent();
Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.image_display);
TextView loginDisplay = (TextView) findViewById(R.id.email_display);
TextView passwordDisplay = (TextView) findViewById(R.id.password_display);
if (intent != null) {
loginDisplay.setText(intent.getStringExtra(EXTRA_LOGIN));
passwordDisplay.setText(intent.getStringExtra(EXTRA_PASSWORD));
image.setImageBitmap(bmp);
}
}
}
目前我正确检索了我的ImageView
,但它是索引中的第一个,我想要选择好的图像。