尝试获取警报对话框以根据所选选项更改文本视图

发布于 2024-11-23 22:12:52 字数 2683 浏览 1 评论 0原文

我希望对话框根据所选选项显示正确的文本(即,如果按下两次,我想要显示文本“您按下两次”),

在第一次按下时,它似乎没有执行任何操作,文本将转到初始化 在第二次按下时,您会发现文本已切换为默认值,

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡紫姑娘! 2024-11-30 22:12:52

抱歉,由于工作量很大,我无法测试您的代码。
然而,查看后我注意到以下内容:

1)我认为项目的位置从索引 0 开始。
2) 也许你在 switch-case 语句中丢失了一些 break

所以,尝试将其替换为:

public void startOpt(int item){
  switch(item){

    case 0:
       current_opt.setDescription("you pressed ONE");
    break;

    case 1:
      current_opt.setDescription("you pressed TWO");
    break;

    case 2:
      current_opt.setDescription("you pressed THREE");
    break;

    default:
      current_opt.setDescription("I am a fart and think you have pressed nothing sadface"); 
  } 
}

希望它对你有帮助!

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 the switch-casestatement

So, try to replace it with:

public void startOpt(int item){
  switch(item){

    case 0:
       current_opt.setDescription("you pressed ONE");
    break;

    case 1:
      current_opt.setDescription("you pressed TWO");
    break;

    case 2:
      current_opt.setDescription("you pressed THREE");
    break;

    default:
      current_opt.setDescription("I am a fart and think you have pressed nothing sadface"); 
  } 
}

Hope it helps you!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文