gpt4 book ai didi

android - 在android中访问动态生成的 View 的元素

转载 作者:行者123 更新时间:2023-11-29 01:10:10 25 4
gpt4 key购买 nike

我通过预定义的 XML 在 android 中生成动态 View 。代码如下。我可以使用 card_identifier sting 为不同布局中的每个元素分配操作。问题是我无法访问动态布局中的元素,例如如果我想使用 sw1.setCheched(true) 在动态 View 1 中设置开关 1,那么如何处理该元素?

布局是这样的: Layout View

代码:

public void threeSwitchLayout(String card_name, String card_identifier) {

// Generate dynamic layout
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// Use demo.xml to create layout
final View addView = layoutInflater.inflate(R.layout.demo, null);

final TextView cardTitle = (TextView)addView.findViewById(R.id.cardTitle);
cardTitle.setText(card_name);

final TextView cardIdentifier = (TextView)addView.findViewById(R.id.cardIdentifier);
cardIdentifier.setText(card_identifier);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 0, 15);
addView.setLayoutParams(layoutParams);

SwitchCompat sw1 = (SwitchCompat)addView.findViewById(R.id.switch1);
SwitchCompat sw2 = (SwitchCompat)addView.findViewById(R.id.switch2);
SwitchCompat sw3 = (SwitchCompat)addView.findViewById(R.id.switch3);

sw1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW1 " + isChecked, Toast.LENGTH_SHORT).show();
}
});

sw2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW2 " + isChecked, Toast.LENGTH_SHORT).show();
}
});

sw3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW3 " + isChecked, Toast.LENGTH_SHORT).show();
}
});

container.addView(addView);
}

demo.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/rounded_corners"
android:layout_margin="5dp"
android:layout_marginBottom="5dp"
android:padding="5dp">

<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cardTitle"
android:textStyle="normal|bold"
android:textSize="25sp"
android:padding="5dp"
android:textAlignment="center"
android:textColor="@android:color/background_dark"
android:layout_weight="1" />

<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cardIdentifier"
android:textSize="12sp" />

</RelativeLayout>

<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">

<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:id="@+id/switch1"
android:text="SW 1 "
android:theme="@style/SCBSwitch" />

<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:id="@+id/switch2"
android:text="SW 2 "
android:theme="@style/SCBSwitch"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:id="@+id/switch3"
android:text="SW 3 "
android:theme="@style/SCBSwitch"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />

</RelativeLayout>

</LinearLayout>

最佳答案

请在下面找到代码 fragment

主 Activity

public class Main2Activity extends AppCompatActivity {

LinearLayout container;
View addView;
RelativeLayout switchLay;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
container = (LinearLayout) findViewById(R.id.container);
}

public void addLayout(View v) {
threeSwitchLayout("Try", "first");
}

public void ChangeState(View v) {
try {
SwitchCompat toChng = (SwitchCompat) switchLay.findViewById(R.id.switch1);
toChng.setChecked(true);
} catch (NullPointerException npe) {
Toast.makeText(this, "please add view first", Toast.LENGTH_SHORT).show();
}
}

public void threeSwitchLayout(String card_name, String card_identifier) {
// Generate dynamic layout
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Use demo.xml to create layout
addView = layoutInflater.inflate(R.layout.demo, null);
final TextView cardTitle = (TextView) addView.findViewById(R.id.cardTitle);
cardTitle.setText(card_name);
final TextView cardIdentifier = (TextView) addView.findViewById(R.id.cardIdentifier);
cardIdentifier.setText(card_identifier);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 0, 15);
addView.setLayoutParams(layoutParams);

switchLay = (RelativeLayout) addView.findViewById(R.id.switchLay);
SwitchCompat sw1 = (SwitchCompat) addView.findViewById(R.id.switch1);
SwitchCompat sw2 = (SwitchCompat) addView.findViewById(R.id.switch2);
SwitchCompat sw3 = (SwitchCompat) addView.findViewById(R.id.switch3);

sw1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW1 " + isChecked, Toast.LENGTH_SHORT).show();
}
});

sw2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW2 " + isChecked, Toast.LENGTH_SHORT).show();
}
});

sw3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(getBaseContext(), cardIdentifier.getText().toString() + ":SW3 " + isChecked, Toast.LENGTH_SHORT).show();
}
});

container.addView(addView);
}
}

activity_main2.xml(MainActivity的布局文件)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:onClick="addLayout"
android:text="add layout" />

<Button
android:id="@+id/btnChangeState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="ChangeState"
android:text="Change switch state" />

<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/btnAdd"
android:orientation="vertical"></LinearLayout>


</RelativeLayout>

解释我所做的是,我只是给开关 View 的父布局一个 ID,然后在 java 类中全局引用它们(RelativeLayout switchLay),同样声明你的“addview”,然后访问我们想要更新的开关这些全局 View 。

虽然这只是一个 fragment ,但您可能需要根据您的具体要求更新代码。

关于android - 在android中访问动态生成的 View 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43732172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com