Java 更改布局的组件
我是android初学者,我有一个Java 更改布局的组件,java,android,xml,Java,Android,Xml,我是android初学者,我有一个gridview图像,当你点击图像时,它会将你移动到SecondActivity。在SecondActivity中,我有一些与每个图像相关的信息。SecondActivity的布局对于所有图像都是相同的,我想更改同一布局中每个图像的标题、文本和图像。 我使用了setTitle、setxt和setImageDrawable(),但它没有改变。我会的,但我的一些代码行,也许它将是完全错误的,所以如果它是,请引导我到一个简单的源代码 在产品类别中 public int
gridview
图像,当你点击图像时,它会将你移动到SecondActivity
。在SecondActivity
中,我有一些与每个图像相关的信息。SecondActivity
的布局对于所有图像都是相同的,我想更改同一布局中每个图像的标题、文本和图像。
我使用了setTitle
、setxt
和setImageDrawable()
,但它没有改变。我会的,但我的一些代码行,也许它将是完全错误的,所以如果它是,请引导我到一个简单的源代码
在产品类别中
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
在主活动中
public List<Product> getProductList() {
//pseudo code to get product, replace your code to get real product here
productList = new ArrayList<>();
productList.add(new Product(R.drawable.ii1, " Story Name", "This is description 1"));
productList.add(new Product(R.drawable.i2, "Story Name", "This is description 2"));
productList.add(new Product(R.drawable.i3, "Story Name", "This is description 3"));
productList.add(new Product(R.drawable.i4, "Story Name", "This is description 4"));
productList.add(new Product(R.drawable.i5, "Story Name", "This is description 5"));
productList.add(new Product(R.drawable.i6, "Story Name", "This is description 6"));
productList.add(new Product(R.drawable.i8, "Story Name", "This is description 8"));
productList.add(new Product(R.drawable.i9, "Story Name", "This is description 9"));
productList.add(new Product(R.drawable.i10, "Story Name", "This is description 10"));
return productList;
}
mToolbar = findViewById(R.id.toolbar);
img = findViewById(R.id.imageView6);
t=findViewById(R.id.textView4);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
mToolbar.setTitle(bundle.getString("title"));
if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.ii1));
t.setText(bundle.getString("title"));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")){
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i2));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i3));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i4));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i5));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i6));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i8));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i9));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i10));
}
}
}
}
Intent i= new Intent(MainActivity.this,SecondActivity.class);
Bundle b= new Bundle();
b.putString("Story Name", productList.get(position).getTitle().toString());
i.putExtras(b);
startActivity(i);
TextView t =findViewById(R.id.textView4);
Bundle b=getIntent().getExtras();
String story=b.getString("Story Name");
t.setText(story);
第二个活动XML
<ImageView
android:id="@+id/imageView6"
android:layout_width="160dp"
android:layout_height="180dp"
android:layout_marginLeft="13dp"
android:layout_marginStart="13dp"
android:layout_marginTop="60dp"
app:srcCompat="@drawable/ii1"
tools:ignore="ContentDescription"
android:layout_below="@+id/appBarLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView6"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="22dp"
android:layout_toRightOf="@+id/imageView6"
android:textSize="14sp"
tools:ignore="HardcodedText,RtlHardcoded" />
单击一个图像时,转到单击事件并保持图像的位置。然后将列表中的信息发送到第二个活动。这正在与我一起工作
我将文本发送到第二个活动,如下所示
main活动
public List<Product> getProductList() {
//pseudo code to get product, replace your code to get real product here
productList = new ArrayList<>();
productList.add(new Product(R.drawable.ii1, " Story Name", "This is description 1"));
productList.add(new Product(R.drawable.i2, "Story Name", "This is description 2"));
productList.add(new Product(R.drawable.i3, "Story Name", "This is description 3"));
productList.add(new Product(R.drawable.i4, "Story Name", "This is description 4"));
productList.add(new Product(R.drawable.i5, "Story Name", "This is description 5"));
productList.add(new Product(R.drawable.i6, "Story Name", "This is description 6"));
productList.add(new Product(R.drawable.i8, "Story Name", "This is description 8"));
productList.add(new Product(R.drawable.i9, "Story Name", "This is description 9"));
productList.add(new Product(R.drawable.i10, "Story Name", "This is description 10"));
return productList;
}
mToolbar = findViewById(R.id.toolbar);
img = findViewById(R.id.imageView6);
t=findViewById(R.id.textView4);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
mToolbar.setTitle(bundle.getString("title"));
if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.ii1));
t.setText(bundle.getString("title"));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")){
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i2));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i3));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i4));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i5));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i6));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i8));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i9));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i10));
}
}
}
}
Intent i= new Intent(MainActivity.this,SecondActivity.class);
Bundle b= new Bundle();
b.putString("Story Name", productList.get(position).getTitle().toString());
i.putExtras(b);
startActivity(i);
TextView t =findViewById(R.id.textView4);
Bundle b=getIntent().getExtras();
String story=b.getString("Story Name");
t.setText(story);
第二活动
public List<Product> getProductList() {
//pseudo code to get product, replace your code to get real product here
productList = new ArrayList<>();
productList.add(new Product(R.drawable.ii1, " Story Name", "This is description 1"));
productList.add(new Product(R.drawable.i2, "Story Name", "This is description 2"));
productList.add(new Product(R.drawable.i3, "Story Name", "This is description 3"));
productList.add(new Product(R.drawable.i4, "Story Name", "This is description 4"));
productList.add(new Product(R.drawable.i5, "Story Name", "This is description 5"));
productList.add(new Product(R.drawable.i6, "Story Name", "This is description 6"));
productList.add(new Product(R.drawable.i8, "Story Name", "This is description 8"));
productList.add(new Product(R.drawable.i9, "Story Name", "This is description 9"));
productList.add(new Product(R.drawable.i10, "Story Name", "This is description 10"));
return productList;
}
mToolbar = findViewById(R.id.toolbar);
img = findViewById(R.id.imageView6);
t=findViewById(R.id.textView4);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
mToolbar.setTitle(bundle.getString("title"));
if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.ii1));
t.setText(bundle.getString("title"));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")){
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i2));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i3));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i4));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i5));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i6));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i8));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i9));
}
else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i10));
}
}
}
}
Intent i= new Intent(MainActivity.this,SecondActivity.class);
Bundle b= new Bundle();
b.putString("Story Name", productList.get(position).getTitle().toString());
i.putExtras(b);
startActivity(i);
TextView t =findViewById(R.id.textView4);
Bundle b=getIntent().getExtras();
String story=b.getString("Story Name");
t.setText(story);
我也在图片和标题上使用它,当我发送它时,它没有完成。对不起,我的错误您的意思是我将使用intent传递信息,然后putExtra并启动活动Yes。这就是我要说的