描述:点击button,弹出对话框;另外ImageView显示图片
包含四个文件:main.xml,MainActivity,androidManifest.xml
1.main.xml文件内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/button1" >
</Button>
<ImageView
android:layout_width="200dp"
android:layout_height="100dp"
android:id="@+id/imageView1">
</ImageView>
</LinearLayout>
2.MainActivity
public class MainActivity extends Activity {
public Button mButton;
public ImageView mImageView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button) findViewById(R.id.button1);
mImageView = (ImageView) findViewById(R.id.imageView1);
mImageView.setImageResource(R.drawable.ic_launcher);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Dialog alertDialog = new AlertDialog.Builder(MainActivity.this).
setTitle(R.string.dialog_title).
setMessage(R.string.dialog_content).
setIcon(R.drawable.ic_launcher).
setPositiveButton(R.string.button_content, null).
setNegativeButton("Cancel", null).
create();
alertDialog.show();
}
});
}
}
3.string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TestAlertDialog</string>
<string name="title_activity_main">MainActivity</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="dialog_title">演示框</string>
<string name="dialog_content">Hello, this world</string>
<string name="button_content">确定</string>
</resources>
4.androidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xinhua.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>