0

我有一个名为 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();        
}
 }
4

2 回答 2

2

当您打开另一个 Activity 时,您的 ConvertActivity 不会暂停或销毁,它只是“坐”在那里。查看 startActivityForResult 方法并等待结果,例如,请参见此处。

于 2013-03-08T14:10:24.283 回答
0

要在关闭对话框时触发事件,您需要一个“关闭时”侦听器。这里已经回答了:当 Android Dialog 被解除时,你能触发一个事件吗?

于 2014-09-11T10:38:12.487 回答