views:

56

answers:

1

I am making my TextView clickable and want the text to change as I click on it. So the TextView starts out saying "Al" and when its clicked I want it to change to say, let just say "Hi" for now. And then also go back when clicked again. Here is my code.

package table.periodic;

import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView;

public class Aluminum extends Activity{ public Button next; public TextView symbol; public static final int ONE_ID = Menu.FIRST+1; public static final int TWO_ID = Menu.FIRST+2; public static final int THREE_ID = Menu.FIRST+3; public static final int FOUR_ID = Menu.FIRST+4; public static final int FIVE_ID = Menu.FIRST+5; public static final int SIX_ID = Menu.FIRST+6;

public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.aluminum); symbol = (TextView) findViewById(R.id.symbol);
symbol.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { symbol = (TextView) findViewById(R.id.symbol); if ("Al".equals(symbol.getText())); symbol.setText("Hi"); if ("Hi".equals(symbol.getText())); symbol.setText("Al");

  }  });

next = (Button) findViewById(R.id.next);

next.setOnClickListener(new View.OnClickListener() {
 public void onClick(View view) {
  Intent next = new Intent(Aluminum.this,

Aluminum2.class); startActivity(next);}});

  }    public void onCreateContextMenu(ContextMenu menu,

View v, ContextMenu.ContextMenuInfo menuInfo){ populateMenu(menu); }

public boolean onCreateOptionsMenu(Menu menu){
 populateMenu(menu);

 return(super.onCreateOptionsMenu(menu));

}
public boolean onOptionsItemSelected(MenuItem item){
 return(applyMenuChoice(item)||
   super.onOptionsItemSelected(item));

}
public boolean onContextItemSelected(MenuItem item){
 return(applyMenuChoice(item)||
   super.onContextItemSelected(item));
}
private void populateMenu(Menu menu){
 menu.add(Menu.NONE, ONE_ID, Menu.NONE, "Help");
 menu.add(Menu.NONE, TWO_ID, Menu.NONE, "Home");
 menu.add(Menu.NONE, THREE_ID, Menu.NONE, "List");
 menu.add(Menu.NONE, FOUR_ID, Menu.NONE, "Table");
}

private boolean applyMenuChoice(MenuItem item){
 switch (item.getItemId()){

 case ONE_ID:

  Intent help = new Intent(this, Help.class);
  startActivity(help);

  return(true);

 case TWO_ID:

  Intent table = new Intent(this, table.class);
  startActivity(table);

  return(true);

 case THREE_ID:

  Intent list = new Intent(this, List.class);
  startActivity(list);

  return(true);

 case FOUR_ID:
  Intent Classic = new Intent(this, Classic.class);
  startActivity(Classic);

  return(true);

 }   return false;
}
 }
A: 

You're setting symbol to R.id.symbol inside your click listener. This will reset it to what's in the XML file. Calling setText doesn't change the value that was in R.id.symbol. There's no need for the symbol = (TextView) findViewById(R.id.symbol) inside the onclicklistener

Falmarri
I have removed that and did not have it in there when I first started. It still does not change the text though. It remains Al.
Joshua Sutherland
Oh. try "Al".equals(symbol.getText().toString())
Falmarri
Thanks for the help
Joshua Sutherland
So, what do I do if the cursor does not show? There is no way for someone wanting to just use a trackball to tell which element they have selected. Is there a way to fix this?
Joshua Sutherland