动态添加窗口或对话框?

时间:2013-11-30 04:31:00

标签: java android

我想创建一个小消息框,每次用户按下某个按钮时弹出。每次按下按钮时,有没有办法让我显示一个简单的消息框?这是我的代码。我创建了一个函数,使用click方法传递的信息绘制对话框:

        inventoryList.setClickable(true);
    inventoryList
            .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {
                    localFile file = (localFile) inventoryList
                            .getItemAtPosition(position);
                    showItem(file);
                }
            });
}

public void showItem(localFile file) {
    TextView wpnName = (TextView) weaponDialog.findViewById(R.id.itemName);
    TextView wpnDmg = (TextView) weaponDialog.findViewById(R.id.wpnDamage);
    TextView wpnWei = (TextView) weaponDialog.findViewById(R.id.wpnWeightA);
    TextView wpnRar = (TextView) weaponDialog.findViewById(R.id.wpnRarity);
    wpnName.setText(file.name);
    String sizeStr = String.valueOf(file.size);
    wpnDmg.setText(sizeStr);
    wpnWei.setText(sizeStr);
    weaponDialog.show();
}

感谢您的时间:)

2 个答案:

答案 0 :(得分:1)

    Button yourButton= (Button) findViewById(R.id.yourbuttonId); // your created button

     //what happens when touching the specific Button

    yourButton.setOnClickListener(new view.OnClickListener() { 


    @Override

    public void onClick(View v) {

    Dialog obj = new Dialog (this);   // Define New Dialog Named obj

    obj.setTitle("your dialog title)");  //Naming The Dialog title

    TextView tv = new TextView (this); // Creating TextView object to use it inside the Dialog 

    tv.setText("your Text to be shown in the Dialog"); // Setting the TextView Text 

    obj.setContentView(tv); // setting the TextView Content inside your Dialog

    obj.show(); // Method to show the dialog 

        // TODO Auto-generated method stub

    }
});

以下是The Code Hope我帮助了一点:)

答案 1 :(得分:0)