【问题标题】:use "onclick" in properties in layout for dialogs android在对话框android布局的属性中使用“onclick”
【发布时间】:2014-03-01 16:55:25
【问题描述】:

我有一个这样的活动:

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_bank.xml 中,我有大约 20 个图像,我想将它们的 onClick 字段(在属性窗口的布局中)设置为一个方法。 问题是找不到我的方法,因为这个方法应该在布局的活动中,但这是一个对话框,没有任何活动 请帮助我如何使用onClick?

【问题讨论】:

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

标签: android android-layout


【解决方案1】:

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

请按照此步骤操作。

1 Android 布局文件

文件: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>

文件:res/layout/custom.xml

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

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF" 
        android:layout_toRightOf="@+id/image"/>/>

     <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text=" Ok "
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/image"
        />

</RelativeLayout>
  1. 活动

文件:MainActivity.java

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();
          }
        });
    }
}

祝你好运!

【讨论】:

    【解决方案2】:

    解决方法..

        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;
        }
    }
    

    见here

    【讨论】:

      猜你喜欢
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多