Вот мой пакет кода com.dialog;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.dialog, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText name=(EditText)findViewById(R.id.name);
final EditText contact=(EditText)findViewById(R.id.contact);
final HashMap<String,String> map=new HashMap<String,String>();
Button button_add=(Button)findViewById(R.id.main_add);
final AlertDialog alert;
builder.setCancelable(true)
.setPositiveButton("Add",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
map.put(contact.getText().toString(), name.getText().toString());
//Toast.makeText(Main.this,map.get(contact.getText().toString()), Toast.LENGTH_LONG).show();
dialog.cancel();
}
})
.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setView(textEntryView);
alert=builder.create();
button_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alert.show();
}
});
}
}
Это мои два файла XML:
1) main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/main_add" android:text="Add"></Button>
</LinearLayout>
2) dialog.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">
<TextView
android:id="@+id/name_label"
android:text="Name:"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/name"
android:layout_below="@+id/name_label"
android:hint="Enter your name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/contact_label"
android:layout_below="@+id/name"
android:text="Contact:"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/contact"
android:layout_below="@+id/contact_label"
android:hint="Enter the contact number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Помогите мне !!!