首页 文章

从Android中的自定义Dialog获取 Value

提问于
浏览
0

我正在使用自定义适配器在listview中显示联系人,我创建onClick功能,当任何项目点击时打开自定义对话框 . 之后,我想从对话框中获取联系人号码,但是当我尝试获取错误弹出窗口时 .

IllegalStateException: Could not execute method for android:onClick

Custom Dialog From Custom Adapter

// Other code
// This code is working fine problem is in activity class
public void onClick(View v) {
              Toast.makeText(context, "Item click", Toast.LENGTH_SHORT).show();
              String phoneNumber = phone.getText().toString();
              String userName = name.getText().toString();
              final Dialog dialog = new Dialog(context);
              dialog.setContentView(R.layout.custom_dialog);
              dialog.setTitle(userName);
              EditText etxtContactNumber = (EditText) dialog.findViewById(R.id.etxtContactNumber);
              etxtContactNumber.setText(phoneNumber);
              dialog.show();
            }
// reset of the code

Custom Dialog

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="Name"
    android:ems="10"
    android:id="@+id/etxtContactNumber" />

<Button
    android:text="Send SMS"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btnSendMessage"
    android:onClick="sendMessage" />

<Button
    android:text="Phone Call"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btnPhoneCall"
    android:onClick="phoneCall" />

Main Activity

protected void sendMessage(View view){
    Toast.makeText(this, "Send Message", Toast.LENGTH_SHORT).show();
    EditText etxtContactNumber = (EditText) view.findViewById(R.id.etxtContactNumber);
    String phoneNumber = etxtContactNumber.getText().toString();
    String uri= "smsto:"+phoneNumber;
    Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
    startActivity(intent);
}

我知道错误的原因 etxtContactNumber 不在此视图中 . 这也不在主要活动视图中,然后它是如何获得它的 .
Custom adapter and MainActivity both are two different files

3 回答

  • 0

    您不需要自定义适配器中的etxtContactNumber文本 .

    您必须创建customDialog.java类并在onCreate()下面执行以下代码 .

    setContentView(R.layout.custom_dialog);
    setTitle(userName);
    EditText etxtContactNumber =(EditText)findViewById(R.id.etxtContactNumber);
    etxtContactNumber.setText(phoneNumber);
    

    您必须在自定义适配器的onClick上执行以下代码 .

    final Dialog dialog = new Dialog(context);
    dialog.show();
    

    希望这能解决您的问题 .

  • 0

    如果要在customAdapter中创建 dialog 并想要在MainActivity中获取值,只需创建对话框 public static 并在承包商中初始化它

    Custom Adapter

    public static Dialog dialog;
      customAdapter(Context ctx){
            dialog = new Dialog(ctx);
      }
    

    Main Activity

    dialog.findViewById(R.id.etxtContactNumber);
    
  • 0

    请在下面的行中输入自定义视图

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_filter, parent, false);
    

    然后呢

    EditText etxtContactNumber = (EditText)v.findViewById(R.id.etxtContactNumber);
    

相关问题