我有一个DialogFragment
,有两个按钮和两个文本字段。
我只希望当我在两个文本字段中输入数据并按“确定”按钮时,它可以匹配两个字段的数据并将结果保存到String
。 Toast msg工作正常,但我如何从片段文本字段中获取数据?这是我的Fragment
代码
public class chngpswd extends DialogFragment implements View.OnClickListener {
Button ok,cancel;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View v=inflater.inflate(R.layout.activity_chngpswd,null);
ok=(Button)v.findViewById(R.id.ok);
cancel=(Button)v.findViewById(R.id.cancel);
ok.setOnClickListener(this);
cancel.setOnClickListener(this);
setCancelable(false);
return v;
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.ok)
{
dismiss();
Toast.makeText(getActivity(),"ok", Toast.LENGTH_LONG).show();
}
else {
dismiss();
Toast.makeText(getActivity(),"cancelled", Toast.LENGTH_LONG).show();
}
}
}
Fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.adnaninayat.myapplication.chngpswd"
android:weightSum="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/cancel"
android:layout_marginTop="48dp"
android:layout_below="@+id/pass"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="@+id/ok"
android:layout_alignTop="@+id/cancel"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:ems="10"
android:id="@+id/pass"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Enter New Password"
android:maxLength="4" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:ems="10"
android:id="@+id/cpass"
android:layout_above="@+id/ok"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Confirm Password"
android:maxLength="4" />
</RelativeLayout>
答案 0 :(得分:1)
您必须保存对onCreateView()
中文本字段的引用(就像您使用按钮一样)并使用它们来获取字段数据:
Button ok, cancel;
EditText pass, cpass;
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View v=inflater.inflate(R.layout.activity_chngpswd,null);
ok=(Button)v.findViewById(R.id.ok);
cancel=(Button)v.findViewById(R.id.cancel);
pass = (EditText)v.findViewById(R.id.pass);
cpass = (EditText)v.findViewById(R.id.cpass);
ok.setOnClickListener(this);
cancel.setOnClickListener(this);
setCancelable(false);
return v;
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.ok)
{
dismiss();
String passData = pass.getText().toString();
Toast.makeText(getActivity(),"ok: " + passData, Toast.LENGTH_LONG).show();
}
else {
dismiss();
Toast.makeText(getActivity(),"cancelled", Toast.LENGTH_LONG).show();
}
}
答案 1 :(得分:0)
您可以使用回调机制进行片段到活动的通信。
Communicating between a fragment and an activity - best practices.