2015-10-20 92 views
4

刚才写了这个简单的测试应用程序:一个按钮显示日期和小时,另一个按钮选择随机颜色并显示它。 它可以在模拟器上正常工作,但是当我尝试在真实设备上运行应用程序时,按钮不起作用(不起作用)。在仿真器上工作但不在真实设备上的Android应用程序

有人可以帮我理解为什么吗?

MainActivity.java:

package yuvallevy.allyouneedapp; 

import android.graphics.Color; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

import java.util.Date; 
import java.util.Random; 

public class MainActivity extends AppCompatActivity { 

    private Button btnShowTime; 
    private Button btnRandomColor; 
    private TextView timeText; 
    private TextView randomColorView; 

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

     btnRandomColor = (Button) findViewById(R.id.btnRandomColor); 
     btnShowTime = (Button) findViewById(R.id.btnShowTime); 
     timeText = (TextView) findViewById(R.id.timeText); 
     randomColorView = (TextView) findViewById(R.id.randomColorView); 

     btnShowTime.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       String currentDataTimeString = java.text.DateFormat.getDateTimeInstance().format(new Date()); 
       timeText.setText(currentDataTimeString); 
      } 
     }); 

     btnRandomColor.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Random rnd = new Random(); 
       int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); 
       randomColorView.setBackgroundColor(color); 
      } 
     }); 
    } 
} 

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <Button 
     android:id="@+id/btnShowTime" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@+id/randomColorView" 
     android:layout_toStartOf="@+id/randomColorView" 
     android:text="Show Time" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/timeText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/btnRandomColor" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_toEndOf="@+id/btnRandomColor" 
     android:layout_toRightOf="@+id/btnRandomColor" /> 

    <Button 
     android:id="@+id/btnRandomColor" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@id/btnShowTime" 
     android:text="Random Color" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/randomColorView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/btnRandomColor" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignTop="@+id/btnRandomColor" 
     android:layout_toRightOf="@+id/btnRandomColor" /> 


</RelativeLayout> 

AndoirdManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="yuvallevy.allyouneedapp" > 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme" > 
     <activity android:name=".MainActivity" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+0

在真实设备上测试 - 三星s6 Edge与Android 5.1.1。应用程序的最低API是API 16 4.1 Jelly Bean –

+0

OnClickListener是否被调用? –

+2

我看到你正在使用“android:supportsRtl”,但是你对开始/结束布局属性的使用是不一致的......可能你的TextViews已经定位在按钮下面,因此看不见? –

回答

5

我怀疑这个明显的问题是关系到属性android:supportsRtl="true"和您的设备/环境的不同API级别。

official doc

安卓supportsRtl

声明你的应用是否愿意 支持从右到左(RTL)布局。 如果设置为true,并且 targetSdkVersion设置为17或更高,则系统会激活并使用各种RTL API,以便您的应用可以显示RTL布局。 如果设置为false,或者如果targetSdkVersion设置为16或更低,在RTL 的API将被忽略或不会有任何效果,你的应用程序将表现 相同,而与用户相关联的 区域设置选择布局方向(您的布局将始终从左到右)。

此属性的默认值为false。在API级别17

加入

这个属性,这可能会导致emualtor和设备之间不同的行为。

您需要根据标志修复您的布局,或者尝试删除此标志

1

我不相信从右到左的支持是问题,但我同意矛盾的展示位置属性可能会导致无法预料的问题。

如果我是你,我会摆脱所有令人困惑的相对位置属性,我会用一个更简单的垂直LinearLayout替换RelativeLayout,其中包含两个水平布局(请不要从剪切和粘贴以前的代码,最好从头开始编写这些LinearLayout或使用IDE来初始生成它们)

0

如果Instant Run被激活,这会导致一些类被移动。 要禁用即时运行转到 文件 - >设置 - >生成,执行,部署 - >即时运行 - >取消选中“启用即时运行”

相关问题