尝试获取警报对话框以根据所选选项更改文本视图
我希望对话框根据所选选项显示正确的文本(即,如果按下两次,我想要显示文本“您按下两次”),
在第一次按下时,它似乎没有执行任何操作,文本将转到初始化 在第二次按下时,您会发现文本已切换为默认值,
我是 Android 新手,我认为我不明白该活动在这里做什么。 有人可以帮我找到一种有效的方法吗?
public class AndMainT extends Activity {
private GameLogicT myGame = new GameLogicT();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b_com = (Button)findViewById(R.id.button);
b_com.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CharSequence[] pick_one = {"ONE", "TWO", "THREE"};
call_menu(pick_one);
updateAwesomeText();
}
});
}
public void updateAwesomeText(){
TextView newText = (TextView)findViewById(R.id.text);
newText.setText(myGame.getCurrent_opt().getDescription() + "\n");
}
public void call_menu(CharSequence[] items){
final CharSequence[] f_items = items;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Orders Captain?");
builder.setItems(f_items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
myGame.startOpt(item);
}
});
builder.show();
}
}
public class GameLogicT {
class GOpt{
private CharSequence description = "this is the intialized text sadface";
public CharSequence getDescription() {
return description;
}
public void setDescription(CharSequence description) {
this.description = description;
}
}
private GOpt current_opt = new GOpt();
public void startOpt(int item){
switch(item){
case 1:
current_opt.setDescription("you pressed ONE");
case 2:
current_opt.setDescription("you pressed TWO");
case 3:
current_opt.setDescription("you pressed THREE");
default:
current_opt.setDescription("I am a fart and think you have pressed nothing sadface");
}
}
public GOpt getCurrent_opt() {
return current_opt;
}
public void setCurrent_opt(GOpt current_opt) {
this.current_opt = current_opt;
}
}
我也尝试过,
public void onClick(View v) {
CharSequence[] pick_one = {"ONE", "TWO", "THREE"};
call_menu(pick_one);
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
updateAwesomeText();
}
public void onClick(DialogInterface dialog, int item) {
myGame.startOpt(item);
notify();
}
这会在按下按钮时强制关闭,这不是我的问题,只是说我尝试过!
I want the dialog to show the correct text based on the selected option (ie. If pressed TWO I want is to show the text "you pressed TWO")
on the first press it seemingly does nothing the text goes to the initialized
on the second press you find out the text was switched to default
I'm new to android and I don't think I understand what the activity is doing here.
Can someone please help me find a way that works?
public class AndMainT extends Activity {
private GameLogicT myGame = new GameLogicT();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b_com = (Button)findViewById(R.id.button);
b_com.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CharSequence[] pick_one = {"ONE", "TWO", "THREE"};
call_menu(pick_one);
updateAwesomeText();
}
});
}
public void updateAwesomeText(){
TextView newText = (TextView)findViewById(R.id.text);
newText.setText(myGame.getCurrent_opt().getDescription() + "\n");
}
public void call_menu(CharSequence[] items){
final CharSequence[] f_items = items;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Orders Captain?");
builder.setItems(f_items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
myGame.startOpt(item);
}
});
builder.show();
}
}
public class GameLogicT {
class GOpt{
private CharSequence description = "this is the intialized text sadface";
public CharSequence getDescription() {
return description;
}
public void setDescription(CharSequence description) {
this.description = description;
}
}
private GOpt current_opt = new GOpt();
public void startOpt(int item){
switch(item){
case 1:
current_opt.setDescription("you pressed ONE");
case 2:
current_opt.setDescription("you pressed TWO");
case 3:
current_opt.setDescription("you pressed THREE");
default:
current_opt.setDescription("I am a fart and think you have pressed nothing sadface");
}
}
public GOpt getCurrent_opt() {
return current_opt;
}
public void setCurrent_opt(GOpt current_opt) {
this.current_opt = current_opt;
}
}
I also tried
public void onClick(View v) {
CharSequence[] pick_one = {"ONE", "TWO", "THREE"};
call_menu(pick_one);
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
updateAwesomeText();
}
public void onClick(DialogInterface dialog, int item) {
myGame.startOpt(item);
notify();
}
This causes a force close when the button is pressed, it's not my question just saying I tried!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,由于工作量很大,我无法测试您的代码。
然而,查看后我注意到以下内容:
1)我认为项目的位置从索引 0 开始。
2) 也许你在
switch-case
语句中丢失了一些break
所以,尝试将其替换为:
希望它对你有帮助!
sorry but I can't test your code due a lot of work.
However,after taking a look I noticed the following:
1) I think that position of items starts from index 0.
2) Maybe you lost some
break
in theswitch-case
statementSo, try to replace it with:
Hope it helps you!