首先点击查看

时间:2015-10-20 15:44:53

标签: android

今天我在我的应用中发现了奇怪的行为。我有一个视图(实际上它是TextView),其风格如下:

> <style name="EditTextStyle.Default"
> parent="Base.V12.Widget.AppCompat.EditText">
>     <item name="android:textSize">@dimen/TEXT_SMALL_SIZE</item>
>     <item name="android:padding">@dimen/edit_text_default_padding</item>
>     <item name="android:layout_margin">@dimen/edit_text_default_padding</item>
>     <item name="android:textColor">@color/text.total.color</item>
>     <item name="android:background">@null</item>
>     <item name="android:focusable">true</item>
>     <item name="android:clickable">true</item>
>     <item name="android:focusableInTouchMode">true</item> </style>

我需要处理onClick()操作,只有在onCreate()之后第一次单击此视图时才会出现问题,在其他情况下,所有操作都按预期工作。 android首先点击了第一次点击,另外选择器运行良好,视图突出显示,但是首先点击了onClick()方法。

我已经解决了这个问题没有实现这种风格,但也许有人可以解释我为什么或哪个设置打破了我的应用程序?

2 个答案:

答案 0 :(得分:3)

问题在于以下几行:

<item name="android:focusableInTouchMode">true</item>
<item name="android:focusable">true</item>

由于这些行,第一次点击将用于设置焦点,然后从第二次点击开始,将调用onClick()。

此处有更多信息:Android button - How to set focusable to true and still accept onClick listener on first click?

答案 1 :(得分:0)

如果您从样式设置它,只需删除public void forceUpdate(){ PackageManager packageManager = this.getPackageManager(); PackageInfo packageInfo = null; try { packageInfo =packageManager.getPackageInfo(getPackageName(),0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } String currentVersion = packageInfo.versionName; new ForceUpdateAsync(currentVersion,TodayWork.this).execute(); } public class ForceUpdateAsync extends AsyncTask<String, String, JSONObject> { private String latestVersion; private String currentVersion; private Context context; public ForceUpdateAsync(String currentVersion, Context context){ this.currentVersion = currentVersion; this.context = context; } @Override protected JSONObject doInBackground(String... params) { try { latestVersion = Jsoup.connect("https://play.google.com/store/apps/details?id=" + context.getPackageName()+ "&hl=en") .timeout(30000) .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6") .referrer("http://www.google.com") .get() .select("div.hAyfc:nth-child(3) > span:nth-child(2) > div:nth-child(1) > span:nth-child(1)") .first() .ownText(); Log.e("latestversion","---"+latestVersion); } catch (IOException e) { e.printStackTrace(); } return new JSONObject(); } @Override protected void onPostExecute(JSONObject jsonObject) { if(latestVersion!=null){ if(!currentVersion.equalsIgnoreCase(latestVersion)){ // Toast.makeText(context,"update is available.",Toast.LENGTH_LONG).show(); if(!(context instanceof SplashActivity)) { if(!((Activity)context).isFinishing()){ showForceUpdateDialog(); } } } } super.onPostExecute(jsonObject); } public void showForceUpdateDialog(){ context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName()))); } }

或者如果您从xml设置它,只需删除<item name="android:focusableInTouchMode">true</item>