【问题标题】:try to do a public method尝试做一个公共方法
【发布时间】:2011-04-08 20:36:39
【问题描述】:

我想创建一个方法来在我单击它时打开一个弹出窗口,并且标题和文本将是自动的,如下所示:

    public void Display(String test){



        new AlertDialog.Builder(this).setTitle(getTitle()).setMessage(test).setNeutralButton("close", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

            }
        }).show();

        }

但是 test 是一个字符串,并且 set Message 不接受字符串,它来自一个资源 XML,比如 Strings.xml。所以我不知道该怎么做。 而'getTitle()'我认为它行不通。我的取标题方法是这个。

TextView str = new TextView(this);
        str.setText(parent.getItemAtPosition(position).toString());
        String title = str.getText().toString();

【问题讨论】:

    标签: java android string listview methods


    【解决方案1】:

    setMessage 确实接受字符串。查看文档:

    setMessage(CharSequence message)

    你可以传入一个字符串。你尝试编译你的代码吗?

    【讨论】:

    • 对不起,我不是这个意思,我想把 id 放进去,但是 id 指的是一个字符串...
    • 你能解释一下你在说什么吗?你想在里面放一个ID吗?它是什么”?什么身份证?什么“id”指的是字符串?!
    • 有点像 R.strings.texttodisplay 但我发现 =)
    【解决方案2】:

    public void Display(int ID, int position, AdapterView parent){

            TextView str1 = new TextView(this);
            str1.setText(parent.getItemAtPosition(position).toString());
            String title = str1.getText().toString();
    
            TextView str = new TextView(this);
            str.setText(ID);
            String text = str.getText().toString();
    
            new AlertDialog.Builder(this).setTitle(title).setMessage(text).setNeutralButton("close", new DialogInterface.OnClickListener() {
    
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
    
                }
            }).show();
    
            }
    

    就是这样-_-',我取得了一些进展。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多