当上下文传递给一系列非活动方法

时间:2016-11-19 20:36:25

标签: android android-activity android-alertdialog android-context popupwindow

我将活动上下文从我的主活动传递给OnClickListener,后者又将该上下文(或者我想到的)传递给对话框构建器。 onClickListener可以调用活动方法,但是对话框构建器不能使用getActivity()。可能有更好的方法来实现弹出表,但我是GUI和面向对象编程的初学者。代码如下。

public class MainActivity extends AppCompatActivity {

    private MyClickListener myClickListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myClickListener = new MyClickListener(this);

        TextView textView = (TextView)findViewById(R.id.clicked_text_view);
        textView.setOnClickListener(myClickListener);
   }
}


public class MyClickListener implements View.OnClickListener {

    private MyDialogBuilder dialogBuilder;
    private AlertDialog dialog;
    private Context context;

    public MyClickListener(Context context) {
        this.context = context;
    }

    @Override
    public void onClick(View view) {
        //do some stuff and pass some stuff to dialog builder
        //create a dialog
        dialogBuilder = new MyDialogBuilder(context);
        dialog = dialogBuilder.buildDialog();
        dialog.show();
    }
}


    public class MyDialogBuilder {
    Context context;
    AlertDialog dialog;

    public MyDialogBuilder(Context context){
        this.context = context;
    }

    public AlertDialog buildDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        // inflate a layout and pass it to the dialog builder
        LayoutInflater inflater = getActivity().getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.popup_dialog, null));

        builder.setMessage(R.string.dialog_message)
                                .setTitle("pop up title");
        dialog = builder.create();

        return dialog;
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.applicationwithpopup.MainActivity">

    <TextView android:id="@+id/clicked_text_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="72dp"
        android:layout_gravity="center"
        android:text="click me"/>
</LinearLayout>



<?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">

<TextView
    android:id="@+id/dialog_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="dialog layout"
    android:textSize="@dimen/text_size" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您需要在活动

中创建自己的方法
<a class="button" href="http://www.glamquotes.com/wp-content/uploads/2011/11/smile.jpg" download="smile.jpg">Download image</a>

而不是在你的监听器中调用这个方法:

private void createDialog(Context context) {
    // Create our dialog here
    private MyDialogBuilder dialogBuilder;
    private AlertDialog dialog;

    dialogBuilder = new MyDialogBuilder(context);
    dialog = dialogBuilder.buildDialog();
    dialog.show();
}