我有一个名为 ConvertActivity 的活动,它有一个按钮,它调用 SetPrecisionActivity 对话框。显示对话框后,用户按下关闭按钮后,不会调用 ConvertActivity 的 onResume() 或 onRestart()。返回ConvertActivity后需要做一些处理。
转换活动.java
public class ConvertActivity extends Activity {
private int callingActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.convert_layout);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.setprecision:
// show the precision screen
SetPrecisionActivity.app_launched(this);
break;
}
}
}
转换布局
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*"
android:padding="15dp"
android:background="#ffffff">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="1"
android:gravity="center_horizontal"
android:drawableTop="@drawable/setprecision"
android:onClick="onClick"
android:background="@null"
android:id="@+id/setprecision"
android:textSize="12sp"
android:textColor="#000000"
android:text="@string/csetprecisionh" />
</TableRow>
setprecisionActivity.java(将显示为对话框)
public class SetPrecisionActivity {
private static Spinner spinnerp;
public static void app_launched(Activity mContext) {
SharedPreferences prefs = mContext.getSharedPreferences("helloapp", 0);
SharedPreferences.Editor editor = prefs.edit();
showPrecisionDialog(mContext, editor);
editor.commit();
}
public static void showPrecisionDialog(final Activity mContext, final SharedPreferences.Editor editor) {
final Dialog dialog = new Dialog(mContext);
dialog.setTitle("Set precision ");
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) mContext.findViewById(R.id.popupprecision);
LayoutInflater layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout = layoutInflater.inflate(R.layout.setprecision_layout, viewGroup);
// Getting a reference to Close button, and close the popup when clicked.
Button close = (Button) layout.findViewById(R.id.close);
close.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.setContentView(layout);
dialog.show();
}
}