单击内部警报对话框更改活动

时间:2014-07-19 22:09:30

标签: android alertdialog

所以我想要做的是在点击警报对话框中的某个单词时更改活动。

文本来自JSON文件,显示正常。我只是希望能够从警报对话框中单击一个单词,然后从那里转到另一个活动。关于如何完成这项工作的任何想法?

2 个答案:

答案 0 :(得分:0)

您需要做的是为TextView(包含文本)设置clicklistener

您可以这样做:

public void onCreate(Bundle savedInstanceState){
    ...

    TextView text = new TextView(this);//or findViewById(R.id.textView)
    text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onTextClicked(view);
                }
            });
    ....
}

public void onTextClick(View v) {
    Intent i = new Intent(this, newIntent.class);
    startActivity(i);
}

你可以添加onTextClick方法来接受你需要的参数。

答案 1 :(得分:0)

您可以做的是以编程方式创建视图并将其设置为对话框的内容。

//The Layouts of the dialog
Dialog confirmDialog = new Dialog(this);
LinearLayout btnLayout = new LinearLayout(this); //Contains OK & Cancel button
LinearLayout dialogLayout = new LinearLayout(this); //Container for the dialog

dialogLayout.setOrientation(LinearLayout.VERTICAL);
btnLayout.setOrientation(LinearLayout.HORIZONTAL);
btnLayout.setBackgroundColor(Color.LTGRAY);

//Create TextView and set clickable atributes
String definition = "Can click on each word on this to show a toast".trim();
TextView definitionView = new TextView(this);
definitionView.setMovementMethod(LinkMovementMethod.getInstance());
definitionView.setText(definition, TextView.BufferType.SPANNABLE);
Spannable spans = (Spannable) definitionView.getText();
BreakIterator iterator = BreakIterator.getWordInstance(Locale.US);
iterator.setText(definition);
int start = iterator.first();
for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next()) {
    String possibleWord = definition.substring(start, end);
    if (Character.isLetterOrDigit(possibleWord.charAt(0))) {
        ClickableSpan clickSpan = getClickableSpan(possibleWord);
        spans.setSpan(clickSpan, start, end,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

dialogLayout.addView(definitionView);

//Create Ok and Cancell Button
Button cancelBtn = new Button(this);
cancelBtn.setText(getString(R.string.detail_dialog_cancel_button_text));
cancelBtn.setBackgroundColor(Color.WHITE);
cancelBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            confirmDialog.cancel();
        }
    });
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, getDp(60), 1f);
params.setMargins(0, getDp(1), 0, 0);
cancelBtn.setLayoutParams(params);

Button okBtn = new Button(this);
okBtn.setText(getString(R.string.detail_dialog_confirm_button_text));
okBtn.setBackgroundColor(Color.WHITE);
okBtn.setOnClickListener(new View.OnClickListener() {
        @Override
         public void onClick(View view) {
              confirmDialog.cancel();
         }
    });
params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, getDp(60), 1f);
params.setMargins(getDp(1), getDp(1), 0, 0);
okBtn.setLayoutParams(params);

btnLayout.addView(cancelBtn);
btnLayout.addView(okBtn);
dialogLayout.addView(btnLayout);

confirmDialog.setContentView(dialogLayout);
confirmDialog.setTitle("Click The Words");
confirmDialog.show();

如果您有多个TextView,则可以创建包含TextViews的contentLayout(即LinearLayout),然后将contentLayout添加到dialoglayout。

您需要实现处理clickListener的getClickableSpan()方法..

private ClickableSpan getClickableSpan(final String word) {
    return new ClickableSpan() {
        final String mWord;
        {
            mWord = word;
        }

        @Override
        public void onClick(View widget) {
            Log.d("tapped on:", mWord);
            Toast.makeText(widget.getContext(), mWord, Toast.LENGTH_SHORT)
                    .show();
        }

        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
        }
    };
}

您可以覆盖onClick()方法,然后调用打开新活动的方法