问题描述:

My problem is pretty simple. I am creating a card based on the result of a HTTP query performed inside a separate thread. The card also has an onclick method and is defined inside a runOnUiThread() located inside the separate thread. However, when the device is tapped, the onclick event isn't fired.

Here is my code:

private void login() {

Runnable r = new Runnable() {

@Override

public void run() {

// irrelevant code

runOnUiThread(new Runnable() {

@Override

public void run() {

setContentView(buildError(code));

}

}

}

Thread t = new Thread(r);

t.start();

}

private View buildError(String code) {

CardBuilder card = new CardBuilder(this, CardBuilder.Layout.ALERT);

card.setIcon(R.drawable.ic_warning_150);

if (code.equals("1"))

card.setText("Incorrect credientals");

else

card.setText("Unexpected error");

card.setFootnote("Tap to try again");

View cView = card.getView();

cView.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Log.i("Event", "Clicked"); // This is what isn't triggering

}

});

cView.setFocusable(true);

cView.setFocusableInTouchMode(true);

return cView;

}

网友答案:

Even though the snippet of code contains an error (can't be compiled, missing ; at the Runnable statement), you were on the right track.

The View simply needs to request the focus in order to be clickable right away. Otherwise you'll have to move the focus manually.

cView.setFocusable(true);
cView.setFocusableInTouchMode(true);
cView.requestFocus();

Reference

相关阅读:
Top