Android应用程序在模拟器上工作,但不在真实设备上

Yuv*_*evy 4 android emulation android-studio

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

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

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);
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

bon*_*nyz 7

我怀疑这个明显的问题与android:supportsRtl="true"您的设备/ emualtor 的属性 和不同的API级别有关.

来自官方文件:

机器人:supportsRtl

声明您的应用程序是否愿意支持从右到左(RTL)布局.如果设置为true且targetSdkVersion设置为17或更高,则系统将激活并使用各种RTL API,以便您的应用程序可以显示RTL布局. 如果设置为false或者targetSdkVersion设置为16或更低,则RTL API将被忽略或无效,并且无论与用户的Locale选项关联的布局方向如何,您的应用都将表现相同(您的布局将始终保持不变-to-右).

此属性的默认值为false.

此属性已在API级别17中添加.

这可能会导致emualtor和您的设备之间出现不同的行为.

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