Java 如何管理新创建的非';t上市,安卓工作室
我已经设置了这个活动,当您按下floatingAction按钮时,它会创建一个新的XML按钮并将其添加到LinearLayout。我想管理我创建的这些新按钮,但我不知道如何管理。我需要给每个按钮一个单独的名称吗?或者把它们放在一个按钮阵列中?这是我的代码和xml。希望我解释得足够好 代码:Java 如何管理新创建的非';t上市,安卓工作室,java,android,Java,Android,我已经设置了这个活动,当您按下floatingAction按钮时,它会创建一个新的XML按钮并将其添加到LinearLayout。我想管理我创建的这些新按钮,但我不知道如何管理。我需要给每个按钮一个单独的名称吗?或者把它们放在一个按钮阵列中?这是我的代码和xml。希望我解释得足够好 代码: 公共类片段扩展片段{ 私人线路布局; 私有浮动操作按钮mButton; 私有字符串newCataLine; //私有ArrayList按钮列表=新建ArrayList(); 公共静态实例{ CataFragm
公共类片段扩展片段{
私人线路布局;
私有浮动操作按钮mButton;
私有字符串newCataLine;
//私有ArrayList按钮列表=新建ArrayList();
公共静态实例{
CataFragment=新CataFragment();
返回片段;
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment_cata,容器,
假);
mLayout=(LinearLayout)rootView.findviewbyd(R.id.LinearLineLayout);
mButton=(FloatingActionButton)rootView.findViewById(R.id.plus);
setOnClickListener(onClick());
按钮cataButton=新按钮(getContext());
返回rootView;
}
private View.OnClickListener onClick()的{
返回新视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
showChangeLangDialog();
}
};
}
私有文本视图createNewTextView(字符串文本){
最终LinearLayout.LayoutParams lparams=
新的
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父项,
LinearLayout.LayoutParams.WRAP_内容);
最终按钮cataButton=新按钮(getContext());
Catabuton.setAllCaps(假);
Catabuton.SettexSize(40);
Catabuton.setLayoutParams(lparams);
cataButton.setText(文本);
返回按钮;
}
public void showChangeLangDialog(){
final AlertDialog.Builder dialogBuilder=新建
AlertDialog.Builder(getContext());
LayoutInflater充气机=this.getLayoutInflater();
最终视图对话框视图=充气机。充气(R.layout.custom_对话框,空);
dialogBuilder.setView(dialogView);
最终EditText edt=(EditText)dialogView.findViewById(R.id.edit1);
dialogBuilder.setTitle(“创建新类别”);
dialogBuilder.setPositiveButton(“添加”,新建
DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
newCataLine=edt.getText().toString();
addView(createNewTextView(newCataLine));
}
});
setNegativeButton(“不添加”,新建
DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
}
});
AlertDialog b=dialogBuilder.create();
b、 show();
}
}
Xml:
由于您要在createNewTextView
中创建文本视图的新按钮,您可以创建一个ArrayList
来存储对您创建的所有文本视图的引用,然后您可以通过它们的索引访问它们。如果他们有特殊的ID,你可以使用地图来方便检索 我不知道如何给我的按钮分配id。这个id可能与你在按钮中显示的数据有关,或者你可以使用view.getId()
,但是这些id是系统分配的,没有什么意义。那么你认为我应该怎么做?我对这个概念非常陌生,我在网上找不到太多。也许在你的情况下,使用他们显示的文本地图到文本视图,然后如果你想修改他们,你可以通过他们的文本来查找他们?不过,我不确定你想做什么来管理它们
public class CataFragment extends Fragment {
private LinearLayout mLayout;
private FloatingActionButton mButton;
private String newCataLine;
//private ArrayList<Button> buttonsList = new ArrayList<>();
public static CataFragment newInstance() {
CataFragment fragment = new CataFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_cata, container,
false);
mLayout = (LinearLayout) rootView.findViewById(R.id.LinearLineLayout);
mButton = (FloatingActionButton) rootView.findViewById(R.id.plus);
mButton.setOnClickListener(onClick());
Button cataButton = new Button(getContext());
return rootView;
}
private View.OnClickListener onClick() {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
showChangeLangDialog();
}
};
}
private TextView createNewTextView(String text) {
final LinearLayout.LayoutParams lparams =
new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
final Button cataButton = new Button(getContext());
cataButton.setAllCaps(false);
cataButton.setTextSize(40);
cataButton.setLayoutParams(lparams);
cataButton.setText(text);
return cataButton;
}
public void showChangeLangDialog() {
final AlertDialog.Builder dialogBuilder = new
AlertDialog.Builder(getContext());
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
dialogBuilder.setView(dialogView);
final EditText edt = (EditText) dialogView.findViewById(R.id.edit1);
dialogBuilder.setTitle("Create New Category");
dialogBuilder.setPositiveButton("Add", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
newCataLine = edt.getText().toString();
mLayout.addView(createNewTextView(newCataLine));
}
});
dialogBuilder.setNegativeButton("Don't Add", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
AlertDialog b = dialogBuilder.create();
b.show();
}
}
<android.support.constraint.ConstraintLayout
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="match_parent"
tools:context=".HomeFragment">
<FrameLayout
android:id="@+id/frame_fragmentholder"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/LinearLineLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
android:src="@android:color/holo_blue_dark"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="@+id/InputText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/InputText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:backgroundTint="@android:color/black"
android:cursorVisible="false"
android:inputType="textNoSuggestions"
android:textCursorDrawable="@null"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/plus"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginBottom="24dp"
android:layout_marginEnd="24dp"
android:clickable="true"
android:src="@drawable/plus"
app:backgroundTint="@android:color/holo_blue_light"
app:elevation="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
</FrameLayout>
</android.support.constraint.ConstraintLayout>