单击列表视图后显示带有按钮和内部编辑文本的迷你表单
Show mini form with button and Edittext inside after clicking the listview
我想在点击列表视图后显示一个带有按钮和 Edittext 的迷你表单。
这是来自互联网的代码。在列表视图项目中长按后,如何显示带有按钮和文本框的简单表单。
我正在使用 Android Studio。
public class sample extends Activity{
SimpleAdapter simpleAdpt;
private EditText pass;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample);
initList();
// We get the ListView component from the layout
ListView lv = (ListView) findViewById(R.id.listView);
simpleAdpt = new SimpleAdapter(this, planetsList, android.R.layout.simple_list_item_1, new String[] {"planet"}, new int[] {android.R.id.text1});
lv.setAdapter(simpleAdpt);
List<Map<String, String>> planetsList = new ArrayList<Map<String,String>>();
private void initList() {
planetsList.add(createPlanet("planet", "Mercury"));
planetsList.add(createPlanet("planet", "Venus"));
planetsList.add(createPlanet("planet", "Mars"));
planetsList.add(createPlanet("planet", "Jupiter"));
planetsList.add(createPlanet("planet", "Saturn"));
planetsList.add(createPlanet("planet", "Uranus"));
planetsList.add(createPlanet("planet", "Neptune"));
}
private HashMap<String, String> createPlanet(String key, String name) {
HashMap<String, String> planet = new HashMap<String, String>();
planet.put(key, name);
return planet;
}
您可能希望按如下方式实现 ItemLongClickListener
:
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
//do what you want to do.
//If you want to delete that entry.
lv.remove(arg2);//where arg2 is position of item you click
myAdapter.notifyDataSetChanged();
return true;
}
});
现在,如果您希望 textBox
和按钮显示为 AlertDialog
或者您想导航到另一个页面,这就是问题所在。
如果它应该是一个带有文本框的弹出窗口
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
如果它应该导航到一个页面
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
我想在点击列表视图后显示一个带有按钮和 Edittext 的迷你表单。
这是来自互联网的代码。在列表视图项目中长按后,如何显示带有按钮和文本框的简单表单。
我正在使用 Android Studio。
public class sample extends Activity{
SimpleAdapter simpleAdpt;
private EditText pass;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample);
initList();
// We get the ListView component from the layout
ListView lv = (ListView) findViewById(R.id.listView);
simpleAdpt = new SimpleAdapter(this, planetsList, android.R.layout.simple_list_item_1, new String[] {"planet"}, new int[] {android.R.id.text1});
lv.setAdapter(simpleAdpt);
List<Map<String, String>> planetsList = new ArrayList<Map<String,String>>();
private void initList() {
planetsList.add(createPlanet("planet", "Mercury"));
planetsList.add(createPlanet("planet", "Venus"));
planetsList.add(createPlanet("planet", "Mars"));
planetsList.add(createPlanet("planet", "Jupiter"));
planetsList.add(createPlanet("planet", "Saturn"));
planetsList.add(createPlanet("planet", "Uranus"));
planetsList.add(createPlanet("planet", "Neptune"));
}
private HashMap<String, String> createPlanet(String key, String name) {
HashMap<String, String> planet = new HashMap<String, String>();
planet.put(key, name);
return planet;
}
您可能希望按如下方式实现 ItemLongClickListener
:
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
//do what you want to do.
//If you want to delete that entry.
lv.remove(arg2);//where arg2 is position of item you click
myAdapter.notifyDataSetChanged();
return true;
}
});
现在,如果您希望 textBox
和按钮显示为 AlertDialog
或者您想导航到另一个页面,这就是问题所在。
如果它应该是一个带有文本框的弹出窗口
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
如果它应该导航到一个页面
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);