开发者

Android dev: How to find out which EditText is calling a context menu

I have 3 EditText defined in the XML file. Using code, how do I find out which of those EditText is calling the context menu?

Basically, I need to have a "Help" and "Set To Default" for each EditText. I can get the context menu to be the same for each item, but not different for each item. Here is the code. The specific question is in the ContextChoice method. Case 0 is help, Case 1 is Set to Default. How, in each of these cases, do I find out which EditText is calling that context menu, so I can set different help and different default values, instead of all of them at once?

@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("w9999999开发者_开发问答9");
            fName.setText("Bob");
            lName.setText("Frank");
            return true;
    }
    return false;
}

I hope this is clear. Thank for the help.


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

In this case, the view parameter is the View (and thus EditText) on which the contextmenu was called. More info


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜