我正在开发一个Android应用程序,我的建议是在页面上有两个可点击的文本视图,当用户按下每个textview时,弹出窗口中的列表视图将显示在textview下,当用户选择listview项目时,textview将将setText()作为用户选择的名称,当用户长时间单击textview或listview项时,可以显示上下文菜单以允许用户“编辑”(跳转到新活动)或“删除”选定的项目,然后列表适配器可以相应地更新列表。 我已经为textview完成了registerContextMenu的代码并且它可以工作,但是在我将相同的代码添加到listview后,上下文菜单将不会显示,我也尝试将OnCreateContextMenuListener添加到listview但是再次失败。任何专家都可以帮忙调查一下吗?非常感谢。 这是代码:
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
/**
* @author Allen
*
*
*/
public class ProfileActivity extends Activity {
private TextView aTextView;
private TextView bTextView;
private View popupWindow;
// to initial popupwindow width and height
private final int popupWindowWidth = 400;
private final int popupWindowHeight = 350;
// the background drawable for popupWindow
ColorDrawable blackBackground = new ColorDrawable(Color.BLACK);
private PopupWindow aPW;
private ListView aListView;
private ArrayAdapter<String> aListViewAdapter;
private List<String> popupNameList;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_page);
init();
Toast.makeText(this, "Long press item to modify", Toast.LENGTH_SHORT).show();
}
private void init() {
aTextView = (TextView) findViewById(R.id.aSelectionTextView);
aTextView.setText("Pls select");
aTextView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
//to bTextView will be the same process, only the popupnamelist //will be the different list
popupNameList = new ArrayList<String>();
popupWindow = this.getLayoutInflater().inflate(R.layout.profile_popup_window, null);
aPW = new PopupWindow(popupWindow, popupWindowWidth, popupWindowHeight);
addOnClickListeners();
}
private void aSelection() {
// TODO Auto-generated method stub
popupNameList = new ArrayList<String>();
popupNameList.add("abc");
popupNameList.add("ABC");
popupNameList.add("123");
// to enable the outside click dismiss the PW
aPW.setBackgroundDrawable(blackBackground);
aPW.setOutsideTouchable(true);
// to limit dispatching the click event.
aPW.setFocusable(true);
aListView = (ListView) popupWindow.findViewById(R.id.profile_popup_lstview);
aListViewAdapter = new ArrayAdapter<String>(this, R.layout.profile_listview_item, popupNameList);
aListView.setAdapter(aListViewAdapter);
aListView.setLongClickable(true);
this.registerForContextMenu(aListView);
aListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
aTextView.setText(popupNameList.get(position));
aPW.dismiss();
}
});
aListView.setOnCreateContextMenuListener(mListOnCreateContextMenuListener);
}
private void addOnClickListeners() {
// TODO Auto-generated method stub
aTextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
aSelection();
aPW.showAsDropDown(findViewById(R.id.aSelectionTextView), 0, -10, Gravity.START);
}
});
this.registerForContextMenu(aTextView);
}
public void onLaunchDoneClick(View view) {
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
// menu.setHeaderTitle("Operation");
menu.add(0, 0, 0, getString(R.string.edit));
menu.add(0, 1, 0, getString(R.string.delete));
}
private final OnCreateContextMenuListener mListOnCreateContextMenuListener = new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
Toast.makeText(getApplicationContext(), "onCreateContextMenu()", 1000).show();
menu.add(0, 0, 0, getString(R.string.edit));
menu.add(0, 1, 0, getString(R.string.delete));
}
};
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case 0:
Toast.makeText(getApplicationContext(), popupNameList.get(menuInfo.position), 1000).show();
aListViewAdapter.notifyDataSetChanged();
break;
case 1:
break;
default:
return super.onContextItemSelected(item);
}
return true;
}
}
布局xml在这里:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/bSelectionTextView"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignEnd="@+id/aSelectionTextView"
android:layout_alignStart="@+id/aSelectionTextView"
android:layout_toRightOf="@+id/textView2"
android:layout_below="@+id/aSelectionTextView"
android:gravity="start|center"
android:text="BSelectionTextView"
android:textSize="20sp" />
<Button
android:id="@+id/okButton"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_below="@id/dSelectionTextView"
android:layout_centerHorizontal="true"
android:background="@drawable/my_button_style"
android:gravity="center"
android:onClick="onLaunchDoneClick"
android:text="@string/Done"
android:textSize="18sp"
android:visibility="gone" />
<TextView
android:id="@+id/textView1"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="5dp"
android:layout_alignParentTop="true"
android:gravity="start|center"
android:text="ASelection"
android:textSize="18sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_below="@id/textView1"
android:gravity="start|center"
android:text="BSelection"
android:textSize="18sp" />
<TextView
android:id="@+id/aSelectionTextView"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/textView1"
android:clickable="true"
android:gravity="start|center"
android:text="ASelectionTextView"
android:textSize="20sp" />
</RelativeLayout>