在Android应用程序中,如何在单击其他活动中的按钮时启动新活动(GUI),以及如何在这两个活动之间传递数据?
android android-intent android-lifecycle android-button android-activity
我想做一个button圆角.有没有一种简单的方法可以在Android中实现这一目标?
在我的应用程序"Tide Now WA"中,我最近使用新的Nexus 9平板电脑(Lollipop - API 21)测试了兼容性.
它会写一些按钮文字.此应用程序使用Android 2.3和Android 4.0正确编写文本.即混合资本和小写字母.
当我的Nexus 9上运行相同的应用程序时,文本中的所有字母都大写.
FWIW我的清单包含以下声明:
uses-sdk android:minSdkVersion="10" android:targetSdkVersion="14"
我可以在我的代码中修复此问题,还是操作系统中的错误感谢
按下后退按钮,我希望我的应用程序进入停止状态,而不是被破坏状态.
在Android 文档中,它指出:
...当按下BACK时,并非所有活动都具有被销毁的行为.当用户开始在音乐应用程序中播放音乐然后按BACK时,应用程序将覆盖正常的后退行为,防止播放器活动被破坏,并继续播放音乐,即使其活动不再可见
如何在我自己的应用程序中复制此功能?
我认为必须有三种可能性......
捕获后退按钮(如下所示),然后调用主页按钮调用的任何方法.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "back button pressed");
}
return super.onKeyDown(keyCode, event);
}
Run Code Online (Sandbox Code Playgroud)按下后退按钮,然后按下主页按钮.
捕获后退按钮,然后启动主屏幕的活动,有效地将我的应用程序的活动置于停止状态.
编辑: 我知道服务,并在与此问题相关的应用程序中使用一个.这个问题具体是关于按下后退按钮将Activity置于停止状态而不是销毁状态.
我做了一个按钮,改变了不同状态下的背景,这样:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/btn_location_pressed" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="@drawable/btn_location_pressed"/> <!-- focused -->
<item android:drawable="@drawable/btn_location"/> <!-- default -->
Run Code Online (Sandbox Code Playgroud)
这里的问题是我也试图改变textColor,就像我对drawable一样,但我无法做到.我已经尝试过android:textColor和android:color但是第一个不起作用,而秒数改变了我的背景.
下一个代码是我布局的一部分.关于文本颜色,它仅适用于正常状态文本颜色,因此在按下时不会将其更改为白色
<Button android:id="@+id/location_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:background="@drawable/location"
android:textSize="15sp"
android:textColor="@color/location_color"
android:textColorHighlight="#FFFFFF"
/>
Run Code Online (Sandbox Code Playgroud)
有人有线索吗?
在AppCompat今天发布更新之前,我能够更改Android L中按钮的颜色,但不能更改旧版本.包含新的AppCompat更新后,我无法更改任何一个版本的颜色,当我尝试按钮时就消失了.有谁知道如何更改按钮颜色?
以下图片显示了我想要实现的目标:

白色按钮是默认的,红色按钮是我想要的.
这是我之前做的改变以下按钮颜色的方法styles.xml:
<item name="android:colorButtonNormal">insert color here</item>
Run Code Online (Sandbox Code Playgroud)
并动态地做:
button.getBackground().setColorFilter(getResources().getColor(insert color here), PorterDuff.Mode.MULTIPLY);
Run Code Online (Sandbox Code Playgroud)
我也确实将主题父@android:style/Theme.Material.Light.DarkActionBar改为Theme.AppCompat.Light.DarkActionBar
我对Android开发很新,只是遇到了偏好.我找到了PreferenceScreens并希望用它创建一个登录功能.我唯一的问题是我不知道热,我可以在PreferenceScreen上添加一个"登录"按钮.以下是我的Preference View的样子:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
...
<PreferenceScreen android:title="@string/login" android:key="Login">
<EditTextPreference android:persistent="true" android:title="@string/username" android:key="Username"></EditTextPreference>
<EditTextPreference android:title="@string/password" android:persistent="true" android:password="true" android:key="Password"></EditTextPreference>
</PreferenceScreen>
...
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
Button应位于两个ExitTextPreferences下面.
这个问题有一个简单的解决方案吗?我找到的一个解决方案无法正常工作,因为我使用子偏好屏幕.
我发现我可以这样添加按钮:
<PreferenceScreen android:title="@string/login" android:key="Login">
<EditTextPreference android:persistent="true" android:title="@string/username" android:key="Username"></EditTextPreference>
<EditTextPreference android:title="@string/password" android:persistent="true" android:password="true" android:key="Password"></EditTextPreference>
<Preference android:layout="@layout/loginButtons" android:key="loginButtons"></Preference>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
并且布局文件(loginButtons.xml)看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:weightSum="10"
android:baselineAligned="false" android:orientation="horizontal">
<Button android:text="Login" android:layout_width="fill_parent"
android:layout_weight="5" android:layout_height="wrap_content"
android:id="@+id/loginButton" android:layout_gravity="left"></Button>
<Button android:text="Password?" android:layout_width="fill_parent"
android:layout_weight="5" android:layout_height="wrap_content"
android:id="@+id/forgottenPasswordButton"></Button>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
所以现在按钮出现但我无法在代码中访问它们.我尝试使用findViewById(),但这返回null.我有什么想法可以访问这些按钮?
上面的例子工作正常,因为1.0-rc4版修复了需要不必要变量的问题.
我完全按照文档中的描述进行操作,但它不起作用:
main.xml中:
<layout xmlns:andr...
<data>
</data>
<include layout="@layout/buttons"></include>
....
Run Code Online (Sandbox Code Playgroud)
buttons.xml:
<layout xmlns:andr...>
<data>
</data>
<Button
android:id="@+id/button"
...." />
Run Code Online (Sandbox Code Playgroud)
MyActivity.java:
... binding = DataBindingUtil.inflate...
binding.button; ->cannot resolve symbol 'button'
Run Code Online (Sandbox Code Playgroud)
如何获得按钮?
java data-binding android android-button android-databinding
android ×10
android-button ×10
java ×2
android-view ×1
back ×1
button ×1
data-binding ×1
double-click ×1
overriding ×1