Mih*_*tel -1 android android-edittext android-recyclerview
当用户按下 #button 时,它会如何在 RecyclerView 的焦点 EditText 上打印 #。我的代码和输出如下。请帮我....
我已经做了一切,但被困在这里。我可以得到任何建议或链接,提供有关如何实施的更多信息吗?
活动_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnAt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\@" />
<Button
android:id="@+id/btnHash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\#" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
主活动.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private RecyclerView mRecyclerView; // RecyclerVIew
private CheckItemAdapter myAdapterRecyclerView; //The Adapter for RecyclerVIew
Button btnAt, btnHash;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
setAdapter();
btnAt = (Button) findViewById(R.id.btnAt);
btnHash = (Button) findViewById(R.id.btnHash);
}
public void setAdapter() {
mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); // Set LayoutManager in the RecyclerView
myAdapterRecyclerView = new CheckItemAdapter(this, getData()); // Create Instance of MyAdapterRecyclerView
mRecyclerView.setAdapter(myAdapterRecyclerView); // Set Adapter for RecyclerView
}
public ArrayList<ItemModel> getData() {
ArrayList<ItemModel> itemModelArrayList = new ArrayList<ItemModel>();
ItemModel itemModel = new ItemModel();
itemModel.setChkText("ABC");
itemModelArrayList.add(itemModel);
itemModel = new ItemModel();
itemModel.setChkText("ABC");
itemModelArrayList.add(itemModel);
itemModel = new ItemModel();
itemModel.setChkText("ABC");
itemModelArrayList.add(itemModel);
itemModel = new ItemModel();
itemModel.setChkText("ABC");
itemModelArrayList.add(itemModel);
itemModel = new ItemModel();
itemModel.setChkText("ABC");
itemModelArrayList.add(itemModel);
itemModel = new ItemModel();
itemModel.setChkText("ABC");
itemModelArrayList.add(itemModel);
itemModel = new ItemModel();
itemModel.setChkText("ABC");
itemModelArrayList.add(itemModel);
return itemModelArrayList;
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnAt) {
//print @ on focused EditText of RecyclerView item
} else if (v.getId() == R.id.btnHash) {
//print # on focused EditText of RecyclerView item
}
}
}
Run Code Online (Sandbox Code Playgroud)
检查项适配器
public class CheckItemAdapter extends RecyclerView.Adapter<CheckItemAdapter.MyViewHolder> {
private ArrayList<ItemModel> mList;
Context context;
public CheckItemAdapter(Context context, ArrayList<ItemModel> mList) {
this.mList = mList;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // create a new view
View itemLayoutView = LayoutInflater.from(context).inflate(R.layout.single_check_list, parent, false);
// create ViewHolder
MyViewHolder viewHolder = new MyViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
ItemModel itemModel = mList.get(position);
holder.editCheckItem.setText(itemModel.getChkText());
}
@Override
public int getItemCount() {
if (mList != null)
return mList.size();
else
return 0;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public EditText editCheckItem;
public CheckBox chkItem;
public TextView txtRemove;
public MyViewHolder(View itemLayoutView) {
super(itemLayoutView);
editCheckItem = (EditText) itemLayoutView.findViewById(R.id.editCheckItem);
}
}
}
[![output image][1]][1]
Run Code Online (Sandbox Code Playgroud)
小智 5
在您的活动中将按钮声明为静态。
示例:在 ABCActivity 中:
public static Button btnTest;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_family_member);
btnTest = (Button)findviewbyid(R.id.btnTest);
}
Run Code Online (Sandbox Code Playgroud)
在适配器中:
ABCActivity.btnTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2863 次 |
| 最近记录: |