我有一个RecyclerView,在我想要的最后一个editText上有4个editText,当按Enter时要集中在新的editTexts集的第一个editText上。
我的想法似乎仅在应用程序启动时才适用于第一行。
我尝试过的事情:
EditTextModel.jave
public class EditTextModel {
private String modelCode, modelPerigrafi, modelMM, modelTmx;
public EditTextModel(String modelCode, String modelPerigrafi, String modelMM, String modelTmx) {
this.modelCode = modelCode;
this.modelPerigrafi = modelPerigrafi;
this.modelMM = modelMM;
this.modelTmx = modelTmx;
}
public String getModelCode() {
return modelCode;
}
public void setModelCode(String modelCode) {
this.modelCode = modelCode;
}
public String getModelPerigrafi() {
return modelPerigrafi;
}
public void setModelPerigrafi(String modelPerigrafi) {
this.modelPerigrafi = modelPerigrafi;
}
public String getModelMM() {
return modelMM;
}
public void setModelMM(String modelMM) {
this.modelMM = modelMM;
}
public String getModelTmx() {
return modelTmx;
}
public void setModelTmx(String modelTmx) {
this.modelTmx = modelTmx;
}
}
RecyclerViewAdapter.java
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private LayoutInflater inflater;
public static ArrayList<EditTextModel> modelArray;
private Context mContext;
private MainActivity mainActivity;
private RecyclerView recyclerViewPassed;
public RecyclerViewAdapter(Context mContext, ArrayList<EditTextModel> modelArray, RecyclerView recyclerViewPassed) {
this.mContext = mContext;
inflater = LayoutInflater.from(mContext);
this.modelArray = modelArray;
this.recyclerViewPassed = recyclerViewPassed;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.from(parent.getContext()).inflate(R.layout.rv_row, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
holder.codeET.setText(modelArray.get(position).getModelCode());
holder.perigrafiET.setText(modelArray.get(position).getModelPerigrafi());
holder.mmET.setText(modelArray.get(position).getModelMM());
holder.tmxET.setText(modelArray.get(position).getModelTmx());
holder.tmxET.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event == null || event.getAction() != KeyEvent.ACTION_DOWN)
return false;
modelArray.add(new EditTextModel("", "", "", ""));
notifyDataSetChanged();
recyclerViewPassed.scrollToPosition(modelArray.size());
holder.codeET.requestFocus();
return true;
}
});
holder.rvLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, modelArray.get(position).getModelTmx() + " " + modelArray.get(position).getModelPerigrafi(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return modelArray.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
protected EditText codeET, perigrafiET, mmET, tmxET;
LinearLayout rvLayout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
codeET = itemView.findViewById(R.id.code_et);
perigrafiET = itemView.findViewById(R.id.perigrafi_et);
mmET = itemView.findViewById(R.id.mm_et);
tmxET = itemView.findViewById(R.id.tmx_et);
rvLayout = itemView.findViewById(R.id.rv_layout);
codeET.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
modelArray.get(getAdapterPosition()).setModelCode(codeET.getText().toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
perigrafiET.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
modelArray.get(getAdapterPosition()).setModelPerigrafi(perigrafiET.getText().toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
mmET.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
modelArray.get(getAdapterPosition()).setModelMM(mmET.getText().toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
tmxET.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
modelArray.get(getAdapterPosition()).setModelTmx(tmxET.getText().toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ArrayList<EditTextModel> textModel = new ArrayList<>();
private RecyclerView recyclerView;
private RecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textModel.add(new EditTextModel("code", "perigrafi","mm","tmx"));
recyclerView = findViewById(R.id.recycler_view);
adapter = new RecyclerViewAdapter(this,textModel, recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
}
rv_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rv_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/code_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="#F8F8F8"
android:focusableInTouchMode="true"
android:imeOptions="actionDone"
android:inputType="number"
android:padding="8dp"
android:singleLine="true" />
<EditText
android:id="@+id/perigrafi_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="#F8F8F8"
android:focusableInTouchMode="true"
android:imeOptions="actionDone"
android:inputType="number"
android:padding="8dp"
android:singleLine="true" />
<EditText
android:id="@+id/mm_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="#F8F8F8"
android:focusableInTouchMode="true"
android:imeOptions="actionDone"
android:inputType="number"
android:padding="8dp"
android:singleLine="true" />
<EditText
android:id="@+id/tmx_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="#F8F8F8"
android:focusableInTouchMode="true"
android:imeOptions="actionDone"
android:inputType="number"
android:padding="8dp"
android:singleLine="true" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
那么当在任何一行的最后一个editText上按下Enter时,怎么可能在底部创建一个新行,并专注于该行和这个editText内部呢?在我们的例子中,它是code
editText。