我想点击微调器和下一页,以显示android studio中drawable的图像。
这只显示微调器
public class navigation extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
Spinner end = (Spinner) findViewById(R.id.end_spinner);
//Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> end_adapter = ArrayAdapter.createFromResource(this, R.array.end_point, android.R.layout.simple_spinner_item);
//Specify the layout to use when the list of choices appears
end_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Apply the adapter to the spinner
end.setAdapter(end_adapter);
end.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.end_spinner){
if(position >=1 && position <=16){
TextView mytext = (TextView) view;
Toast.makeText(this,mytext.getText()+" selected",Toast.LENGTH_LONG).show();
Intent PlanIntent = new Intent(navigation.this, AStar.class);
Bundle bundle = new Bundle();
bundle.putInt("classname",position);
PlanIntent.putExtras(bundle);
startActivity(PlanIntent);
}
else {
Toast.makeText(this,"Please select the destination",Toast.LENGTH_LONG).show();
}
}
}
public void onNothingSelected(AdapterView<?> parent)
{
Spinner start = (Spinner) findViewById(end_spinner);
assert start != null;
start.setOnItemSelectedListener(this);
if (start.getId() != end_spinner) {
}
}
@Override
public void onBackPressed(){
startActivity(new Intent(this,MainActivity.class));
finish();
}
}
这是可以显示图像的下一页
public class AStar扩展了AppCompatActivity {
ImageView imageView;
PhotoViewAttacher mAttacher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_astar);
imageView = (ImageView) findViewById(R.id.imageView);
int position = getIntent().getIntExtra("classname", -1);
if(position != -1){
int classImg = classImages[position];
imageView.setImageResource(classImg);
}
mAttacher = new PhotoViewAttacher(imageView);
}
}
我在微调器中有16个类名,我想要做的是当用户点击每个类名时它将显示来自drawable的图片。每个类名都有不同的图像。我的想法是使用if else,但我不知道代码。
答案 0 :(得分:1)
您已经知道如何使用R.array.end_point
,因此为相应的drawable创建相同大小的另一个xml数组。
您也知道如何使用bundle.putInt("classname",position);
,因此您现在需要从意图中获取第二个Activity中的整数。
使用这两个概念,您可以在第二个Activity中的特定位置获取drawable,并相应地绘制图像
imageView = (ImageView) findViewById(R.id.imageView);
int position = getIntent().getIntExtra("classname", -1);
if(position != -1){
// TODO: get some R.array.classImages
int classImg = classImages[position];
imageView.setImageResource(classImg);
}
我的想法是使用if else
这是错误的想法。往上看。没有其他陈述,只有一个if语句,以确保你已获得意图额外。您只需要一个可绘制资源的整数数组