Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用「;onclick";在android对话框布局中的属性中_Android_Android Layout - Fatal编程技术网

使用「;onclick";在android对话框布局中的属性中

使用「;onclick";在android对话框布局中的属性中,android,android-layout,Android,Android Layout,我有一个这样的活动: TextView txt_bank = (TextView) findViewById(R.id.txt_search_bank); txt_bank.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { dialog_bank = new Dialog(Activity_Search2.th

我有一个这样的活动:

TextView txt_bank = (TextView) findViewById(R.id.txt_search_bank);
    txt_bank.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            dialog_bank = new Dialog(Activity_Search2.this);

            dialog_bank.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            dialog_bank.setContentView(R.layout.list_bank);
            dialog_bank.show();
现在在
list\u bank.xml
中,我有大约20个图像,我想将它们的
onClick
字段(在属性窗口的布局中)设置为一个方法。 问题是找不到我的方法,因为该方法应该在layout的活动中,但这是一个对话框,没有任何活动
请帮助我如何使用
onClick

我不确定这是你问题的绝对答案。但我想可能是你的问题的答案变成了这样。比如

请按此步骤操作

1安卓布局文件

文件:res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/buttonShowCustomDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Custom Dialog" />

</LinearLayout>
祝你好运

解决方法

    public class TestDialog extends Dialog implements android.view.View.OnClickListener
{
    protected void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);
        ((Button)findViewById(R.id.dialog_btn_mybutton)).setOnClickListener(this);
    }



public void onClick(View view) 
{
    switch (view.getId())
    {
        case R.id.dialog_btn_mybutton:
            //do stuff
            // dismiss();
            // cancel etc.
        break;
    }
}

请参见

您的问题是什么?更具体地说,我应该在哪里定义我的方法?@s我有一个解决方案,但您必须为此创建单独的对话框。请转到以下问题:我是否理解正确,您的对话框(而不是活动)中有图像,并且要将对话框的图像设置为侦听器?实际上,如果您在XML中设置onClick,您的活动必须扩展OnClickListener,然后才能处理单击。。。当然,也可以在对话框中处理您的图像点击,我可以解释如何,如果这是您想要做的。。。
public class MainActivity extends Activity {

    final Context context = this;
    private Button button;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.buttonShowCustomDialog);

        // add button listener
        button.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View arg0) {

            // custom dialog
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.custom);
            dialog.setTitle("Title...");

            // set the custom dialog components - text, image and button
            TextView text = (TextView) dialog.findViewById(R.id.text);
            text.setText("Android custom dialog example!");
            ImageView image = (ImageView) dialog.findViewById(R.id.image);
            image.setImageResource(R.drawable.ic_launcher);

            Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            dialog.show();
          }
        });
    }
}
    public class TestDialog extends Dialog implements android.view.View.OnClickListener
{
    protected void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);
        ((Button)findViewById(R.id.dialog_btn_mybutton)).setOnClickListener(this);
    }



public void onClick(View view) 
{
    switch (view.getId())
    {
        case R.id.dialog_btn_mybutton:
            //do stuff
            // dismiss();
            // cancel etc.
        break;
    }
}