Android dev:如何找出哪个EditText正在调用上下文菜单

时间:2011-09-20 18:23:34

标签: android contextmenu

我在XML文件中定义了3个EditText。使用代码,我如何找出哪些EditText正在调用上下文菜单?

基本上,我需要为每个EditText设置一个“帮助”和“设置为默认值”。我可以让每个项目的上下文菜单相同,但每个项目没有不同。这是代码。具体问题在ContextChoice方法中。案例0是帮助,案例1设置为默认。在每种情况下,我如何找出哪个EditText正在调用该上下文菜单,因此我可以设置不同的帮助和不同的默认值,而不是一次性设置所有这些?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.updatedata);

    wNum = (EditText) findViewById(R.id.wNum);
    wNum.setOnCreateContextMenuListener(this);
    fName = (EditText) findViewById(R.id.fName);
    fName.setOnCreateContextMenuListener(this);
    lName = (EditText) findViewById(R.id.lName);
    lName.setOnCreateContextMenuListener(this);

    Button Save = (Button) findViewById(R.id.Save);
    Button Cancel = (Button) findViewById(R.id.Cancel);

    Save.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            Toast.makeText(getBaseContext(), wNum.getText().toString() + "\n" + fName.getText().toString() + " " + lName.getText().toString() + "\n Your info has been saved.", Toast.LENGTH_LONG).show();
        }
    });

    Cancel.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            Toast.makeText(getBaseContext(), "Cancelled", Toast.LENGTH_SHORT).show();
            finish();               
        }
    });

    this.registerForContextMenu(wNum);
    this.registerForContextMenu(fName);
    this.registerForContextMenu(lName);

}

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)
{
    super.onCreateContextMenu(menu, view, menuInfo);
    CreateContext(menu);
}

@Override
public boolean onContextItemSelected(MenuItem item)
{
    return ContextChoice(item);
}

private void CreateContext(Menu menu)
{
    MenuItem mnu1 = menu.add(0,0,0,"Help");     
    MenuItem mnu2 = menu.add(0,1,1,"Set To Default");
}

private boolean ContextChoice(MenuItem item)
{
    switch(item.getItemId())
    {
        case 0:

                showDialog(0);


            return true;
        case 1:
            wNum.setText("w99999999");
            fName.setText("Bob");
            lName.setText("Frank");
            return true;
    }
    return false;
}

我希望这很清楚。感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)
{
    super.onCreateContextMenu(menu, view, menuInfo);
    CreateContext(menu);
}

在这种情况下,view参数是调用上下文菜单的View(因此EditText)。 More info

答案 1 :(得分:0)

int selectedView; // declare a variable selectedview

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

            registerForContextMenu(btnPD);
    registerForContextMenu(btnMD);  //apply same contextMenu to multiple views
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Set Appropriate Name");
    menu.add(0, v.getId(), 0, "Rename");   //contextMenu Item


    switch(v.getId())     //identify Which view is calling context menu and                change selectedview value as below:
    {
    case R.id.btnpointData:
        selectedView=1;
        break;
    case R.id.btnMapRecord:
        selectedView=2;
        break;
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {

    if (item.getTitle() == "Rename") {
        Intent i = new Intent(this, ChangeNamesUsingDialogBox.class);
        startActivityForResult(i, 1);
    }

    return super.onContextItemSelected(item);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if (data == null) {
        return;
    }
    String name = data.getStringExtra("name");

    if (selectedView==1)    //use selectedview identifier to act accordingly.
    btnPD.setText(name);
    if(selectedView==2)
        btnMD.setText(name);
    super.onActivityResult(requestCode, resultCode, data);
}