我正在使TextView可单击,并希望在我点击它时更改文本。因此,TextView开始说“Al”,当它点击时我希望它改变说,现在就说“嗨”。然后再次单击时返回。这是我的代码。
package table.periodic;
import android.app.Activity;
import android.content.Intent;
import.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;
}
}
答案 0 :(得分:0)
您在点击侦听器中将符号设置为R.id.symbol。这会将其重置为XML文件中的内容。调用setText不会更改R.id.symbol中的值。 onclicklistener
中不需要symbol = (TextView) findViewById(R.id.symbol)