Android:在TextView上单击打开对话框

时间:2017-08-05 07:42:25

标签: java android dialog textview

我正在尝试在单击TextView时打开一个对话框。我安装了一些代码并收到错误,如下所示。不幸的是我无法自行解决这个问题,因此需要你的帮助。

TextView

<TextView
        android:id="@+id/Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:fontFamily="casual"
        android:gravity="center"
        android:onClick="openOptions"
        android:padding="4dp"
        android:text="@string/optionsText"
        android:textAllCaps="true"
        android:textColor="#000"
        android:textSize="14sp" />

MainActivity.java:

import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    TextView subtotal1, subtotal2, subtotal3, totalScore;
    EditText[] editTexts = new EditText[30];
    int[] allNumbers = new int[30];
    int tempID, sumSub1, sumSub2, sumSub3, totalSum;

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

        subtotal1 = (TextView) findViewById(R.id.subtotal1);
        subtotal2 = (TextView) findViewById(R.id.subtotal2);
        subtotal3 = (TextView) findViewById(R.id.subtotal3);
        totalScore = (TextView) findViewById(R.id.Total);

        for (int i = 0; i < editTexts.length; i++) {
            tempID = getResources().getIdentifier("field" + (i+1), "id", getPackageName());
            editTexts[i] = (EditText) findViewById(tempID);
            editTexts[i].addTextChangedListener(allTextWatcher);
        }
    }

    public void openOptions() {
        DialogFragment newFragment = new OptionsDialogFragment();
        newFragment.show(getSupportFragmentManager(), "options");
    }

}

OptionsDialogFragment.java:

import android.app.AlertDialog;
import android.app.Dialog;
import android.support.v4.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

/**
 * Created by lukas on 14.07.2017.
 */

public class OptionsDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.dialog_message)
                .setItems(R.array.options_array, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // The 'which' argument contains the index position
                        // of the selected item
                    }
                });
        return builder.create();
    }
}

单击TextView时,出现以下错误消息:

08-05 09:22:21.952 31036-31036/com.example.lukas.dicepokersheet E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.example.lukas.dicepokersheet, PID: 31036
                                                                                  Theme: themes:{default=overlay:com.cyngn.hexo, iconPack:com.cyngn.hexo, fontPkg:com.cyngn.hexo, com.android.systemui=overlay:com.cyngn.hexo, com.android.systemui.navbar=overlay:com.cyngn.hexo}
                                                                                  java.lang.IllegalStateException: Could not find method openOptions(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatTextView with id 'Title'
                                                                                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
                                                                                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                                      at android.view.View.performClick(View.java:5204)
                                                                                      at android.view.View$PerformClick.run(View.java:21158)
                                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5461)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

3 个答案:

答案 0 :(得分:2)

使用您的功能,如下面的代码;

  public void openOptions(View v) {
    DialogFragment newFragment = new OptionsDialogFragment();
    newFragment.show(getSupportFragmentManager(), "options");
}

答案 1 :(得分:0)

在onClickListener中使用以下代码

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setTitle("Are you sure?");
    alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            moveTaskToBack(true);
                            Process.killProcess(Process.myPid());
                            System.exit(1);
                        }
                    })

            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    dialog.cancel();

                }
            });

    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();

答案 2 :(得分:0)

public void openOptions(View view) {
        DialogFragment newFragment = new OptionsDialogFragment();
        newFragment.show(getSupportFragmentManager(), "options");
}

您需要将onclick方法签名保持为public void method(View view),然后只有Textview onClick可以检测并调用它。