我有一个Editext和一个Button(在Activity1中),当用户在Edittext中写入数字(n)并在Activity2中单击Button => show(n)ImageView(空但可以从SD卡拍摄图像)。 我该怎么办。任何人都可以帮助我?
在这里,我的代码。但它不起作用?我不知道。:(
活动A
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
Button button;
EditText editext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button =(Button) findViewById(R.id.button1);
editext=(EditText) findViewById(R.id.textview);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, ShowImages.class);
myIntent.putExtra("intVariableName", Integer.parseInt(editext.getText().toString()));
startActivity(myIntent);
}
}
活动B
public class ShowImages extends Activity {
ImageView[] imageView;
private LinearLayout myLInearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_images);
Intent intent1 = getIntent();
int text1 = intent1.getIntExtra("intVariableName",0);
showimage(text1);
}
public void showimage(int b){
myLInearLayout =(LinearLayout) findViewById(R.id.ln1);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
myLInearLayout.setOrientation(LinearLayout.VERTICAL);
for (int i=0;i<b;i++){
imageView[i]=new ImageView(this)
;
imageView[i].setImageResource(R.drawable.sample_2)
;
myLInearLayout.addView(imageView[i]);
}
}}
答案 0 :(得分:0)
使用Intent
将您的号码传递给Activity2同时阅读this guide
传递数据后,从内存中读取图像。您可以使用BitmapFactory
要以编程方式添加ImageView
,您必须从布局中对其进行充气:
imageView[i] = (ImageView)LayoutInflater.from(mContext).inflate(R.layout.image, myLInearLayout, false);
imageView[i].setImageResource(R.drawable.sample_2);
myLInearLayout.addView(imageView[i]);
R.layout.image:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>