如何在Android中创建一个动态数组按钮?
How to create a dynamic array button in Android?
我有一个按钮。我想为按钮编写函数 "onClick" 。
每次单击它时,我都希望在屏幕上创建一个新按钮。
还有要添加到按钮数组列表中的新按钮。
我进行了搜索,但找不到任何答案。所有的答案都在谈论通过拖放创建按钮,而不是动态创建按钮。
谢谢
<?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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click to add"
android:id="@+id/B1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="clicktoaddcode"
/>
</RelativeLayout>
package com.example.yas.dynamicbutton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clicktoaddcode(View V){
Toast.makeText(MainActivity.this, "Created", Toast.LENGTH_SHORT).show();
}
}
您必须使用 ArrayList。
List<Button> btnList = new ArrayList<>();
// Call Your ClickListener
btnList.add(new Button(YourContext));
YourParent.addView(btnList.get(btnList.size() - 1 ));
已编辑
MainActivity.java
public class MainActivity extends AppCompatActivity {
LinearLayout container;
List<Button> btnList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container = (LinearLayout)findViewById(R.id.container);
}
public void addBtn(View v){
btnList.add(new Button(this));
container.addView(btnList.get(btnList.size() - 1 ));
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clickable="true"
android:onClick="addBtn">
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
为你的 XML -
准备好这个
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add one more" />
<LinearLayout
android:orientation="vertical"
android:id="@+id/myLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
</LinearLayout>
</LinearLayout>
</ScrollView>
而这个在Java-
final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.myLinearLayout);
Button button = (Button)findViewById(R.id.myButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button button = new Button(MainActivity.this);
button.setText("A new Button");
linearLayout.addView(button);
}
});
创建无限大小数组的最简单方法是使用字符串生成器。这允许您在运行时添加许多元素。它有 3 个主要组成部分。
- 声明和初始化。
- 增值/追加
- 打印/使用
在 Android Studio 的任意位置使用下面的代码。
//Basic Decleration
StringBuilder myString = new StringBuilder();
//Adding data
myString.append("Hello text <br>");
myString.append("some more data <br>");
myString.append("more data to store <br>");
//Displaying in already created text view
deviceDetails.setText(Html.fromHtml(callLogs+""));
注意:我将其视为 HTML 字符串和“
" 标签将在文本视图中创建下一行。因此,所有添加的数据都将显示在新行中。
我有一个按钮。我想为按钮编写函数 "onClick" 。 每次单击它时,我都希望在屏幕上创建一个新按钮。 还有要添加到按钮数组列表中的新按钮。
我进行了搜索,但找不到任何答案。所有的答案都在谈论通过拖放创建按钮,而不是动态创建按钮。
谢谢
<?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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click to add"
android:id="@+id/B1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="clicktoaddcode"
/>
</RelativeLayout>
package com.example.yas.dynamicbutton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clicktoaddcode(View V){
Toast.makeText(MainActivity.this, "Created", Toast.LENGTH_SHORT).show();
}
}
您必须使用 ArrayList。
List<Button> btnList = new ArrayList<>();
// Call Your ClickListener
btnList.add(new Button(YourContext));
YourParent.addView(btnList.get(btnList.size() - 1 ));
已编辑
MainActivity.java
public class MainActivity extends AppCompatActivity {
LinearLayout container;
List<Button> btnList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container = (LinearLayout)findViewById(R.id.container);
}
public void addBtn(View v){
btnList.add(new Button(this));
container.addView(btnList.get(btnList.size() - 1 ));
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clickable="true"
android:onClick="addBtn">
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
为你的 XML -
准备好这个<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add one more" />
<LinearLayout
android:orientation="vertical"
android:id="@+id/myLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
</LinearLayout>
</LinearLayout>
</ScrollView>
而这个在Java-
final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.myLinearLayout);
Button button = (Button)findViewById(R.id.myButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button button = new Button(MainActivity.this);
button.setText("A new Button");
linearLayout.addView(button);
}
});
创建无限大小数组的最简单方法是使用字符串生成器。这允许您在运行时添加许多元素。它有 3 个主要组成部分。
- 声明和初始化。
- 增值/追加
- 打印/使用
在 Android Studio 的任意位置使用下面的代码。
//Basic Decleration
StringBuilder myString = new StringBuilder();
//Adding data
myString.append("Hello text <br>");
myString.append("some more data <br>");
myString.append("more data to store <br>");
//Displaying in already created text view
deviceDetails.setText(Html.fromHtml(callLogs+""));
注意:我将其视为 HTML 字符串和“ " 标签将在文本视图中创建下一行。因此,所有添加的数据都将显示在新行中。