gpt4 book ai didi

android - 如何在 Activity 中添加可变数量的 TextViews 和 EditViews

转载 作者:搜寻专家 更新时间:2023-11-01 08:22:33 25 4
gpt4 key购买 nike

我目前正在尝试将用户定义数量的 TextView 和 EditText 添加到 Activity 中,但除了通过创建各种不同的 Activity 对其进行硬编码之外似乎无法做到这一点。

此 Activity 的目的是获取玩家的姓名,其数量由前一个 Activity 的额外 Intent 传递。

我正在尝试添加一个 TextView 说“玩家 X:”和一个 EditText 来为每个玩家键入玩家的名字

我从这篇文章中知道:How to create a variable number of textviews in android我必须以编程方式执行此操作,但是,它似乎对我不起作用(测试时该 Activity 仍为空白)

我已经尝试创建一个临时 LinearLayout,我在 for() 中将两个 View 添加到其中,但仍然没有。

有什么想法吗?我走在正确的轨道上吗?

最好的问候

[编辑] 代码在这里:

public class players extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_players);

Bundle extras = new Bundle();
final int numPlayers = extras.getInt("num");
final LinearLayout myLayout = findViewById(R.id.lin_Re);
final ArrayList<Player> players = new ArrayList<>();



int size = numPlayers; // total number of TextViews to add

TextView[] tv = new TextView[size];
TextView temp;
EditText[] et = new EditText[size];
EditText temp2;
LinearLayout temp3;

for (int i = 0; i < size; i++)
{
temp = new TextView(this);
temp2 = new EditText(this);
temp3 = new LinearLayout(this);

temp.setText("Player " + i + " : "); //arbitrary task

// add the textview to the linearlayout
temp3.addView(temp);
temp3.addView(temp2);

tv[i] = temp;
et[i] = temp2;

myLayout.addView(temp3);
}


<?xml version="1.0" encoding="utf-8"?>
<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">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:id="@+id/lin_Re">



</LinearLayout>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b_send"/>

</LinearLayout>

最佳答案

有两种方法可以实现:
1. 使用RecyclerView [推荐]
2. 将 TextViewEditText(嵌套在 Horizo​​ntal LinearLayout 中)添加到 Vertical LinearLayout 嵌套在ScrollView 以编程方式

如果您熟悉 RecyclerViewListView,我在下面描述的第一个解决方案非常简单,第二个解决方案(您当前的轨道)有点棘手,但仍然可以实现。

解决方案一:
enter image description here
enter image description here
enter image description here

主 Activity

public class MainActivity extends AppCompatActivity {

RecyclerView mPlayerList;
List<String> mPlayerNames;
PlayerAdapter mAdapter;
EditText mInput;
Button mCreateButton;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mPlayerNames = new ArrayList<>();

// setup recycler view
mPlayerList = findViewById(R.id.player_list);
mPlayerList.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new PlayerAdapter();
mPlayerList.setAdapter(mAdapter);

// setup input EditText
mInput = findViewById(R.id.input);

// setup Create button
mCreateButton = findViewById(R.id.create_button);
mCreateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// clear old player names
mPlayerNames.clear();

// read user input: number of player:
String input = mInput.getText().toString();
int numberOfPlayer;
try {
numberOfPlayer = Integer.parseInt(input);
} catch (NumberFormatException e) {
Toast.makeText(MainActivity.this, "Invalid input!!!", Toast.LENGTH_SHORT).show();
return;
}
for (int i = 0; i < numberOfPlayer; ++i) {
mPlayerNames.add("Player #" + (i + 1));
}

// make change on recycler view
mAdapter.notifyDataSetChanged();

// dismiss keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mInput.getWindowToken(), 0);

}
});
}

private class PlayerAdapter extends RecyclerView.Adapter<PlayerAdapter.PlayerHolder> {


@Override
public PlayerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_layout, parent, false);
return new PlayerHolder(v);
}


@Override
public void onBindViewHolder(PlayerHolder holder, int position) {
holder.bind(mPlayerNames.get(position));
}

@Override
public int getItemCount() {
return mPlayerNames.size();
}

public class PlayerHolder extends RecyclerView.ViewHolder {

TextView mPlayerLabel;
EditText mPlayerName;


public PlayerHolder(View itemView) {
super(itemView);
mPlayerLabel = itemView.findViewById(R.id.player_label);
mPlayerName = itemView.findViewById(R.id.player_name);
}

public void bind(String playerName) {
mPlayerLabel.setText(playerName);
mPlayerName.setHint("Name of " + playerName);
}
}
}
}

示例项目可以在这里找到:
https://github.com/raiytu4/stackcase004

关于android - 如何在 Activity 中添加可变数量的 TextViews 和 EditViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48964221/

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