I'm quite new to Android Development and just came across Preferences. I found PreferenceScreens and wanted to create a login functionality with it. The only problem I have it that I don't know hot i could add a "Login" button to the PreferenceScreen. Here's how my Preference View looks like:
我对Android的开发很陌生,只是遇到了偏好。我找到了PreferenceScreens,并希望用它创建一个登录功能。唯一的问题是,我不知道热,我可以添加一个“登录”按钮到首选项。我的偏好视图是这样的:
<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>
The Button should be right under the two ExitTextPreferences.
按钮应该在两个ExitTextPreferences中。
Is there a simple solution for this problem? The one solution i found was not working because i use sub Preference Screens.
这个问题有简单的解决办法吗?我发现的一个解决方案是无效的,因为我使用子首选项屏幕。
Update:
I figured out that i can add buttons this way:
我发现我可以这样添加按钮:
<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>
and the layout file (loginButtons.xml) looks that way:
布局文件(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>
So now the buttons appear but I can't access them in code. I tried it with findViewById() but this is returning null. Any ideas how i could access these buttons?
现在按钮出现了,但是我不能用代码访问它们。我用findViewById()尝试过,但它返回null。有什么办法可以访问这些按钮吗?
8 个解决方案
#1
254
For the xml:
xml:
<Preference android:title="Acts like a button"
android:key="@string/myCoolButton"
android:summary="This is a cool button"/>
Then for the java in your onCreate()
然后对于onCreate()中的java
Preference button = findPreference(getString(R.string.myCoolButton));
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
//code for what you want it to do
return true;
}
});
This will appear like a normal Preference, with just a title and a summary, so it will look like it belongs.
这将看起来像一个普通的首选项,只有一个标题和一个摘要,因此它看起来像是属于它的。
Edit: I changed to using @string/myCoolButton
and getString(R.string.myCoolButton)
because it's best to use resources for compiler assistance, language translation, and ease of String changes.
编辑:我改为使用@string/myCoolButton和getString(R.string.myCoolButton),因为它最好使用资源进行编译器辅助、语言转换和轻松的字符串更改。
#2
36
I wanted to add an Exit link to the preferences and was able to modify Jakar's code to make it work like this:
我想添加一个退出链接到首选项,并能够修改Jakar的代码使其工作如下:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Settings">
<Preference android:title="Click to exit" android:key="exitlink"/>
</PreferenceCategory>
</PreferenceScreen>
Originally the 'Preference' was a 'EditTextPreference' which I hand edited.
最初,“偏好”是“EditTextPreference”,是我亲手编辑的。
Then in the class:
然后在课堂上:
public class MyPreferences extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.mypreferences);
Preference button = (Preference)getPreferenceManager().findPreference("exitlink");
if (button != null) {
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference arg0) {
finish();
return true;
}
});
}
}
}
#3
33
I suppouse its too late. This is what i have done for a Button Preference.
我觉得太晚了。这就是我对按钮首选项所做的。
The preference in preference.xml file in xml folder
偏好的偏好。xml文件夹中的xml文件
....
<Preference
android:key="resetBD"
android:title="@string/ajustes_almacenamiento"
android:summary="@string/ajustes_almacenamiento_desc"
android:widgetLayout="@layout/pref_reset_bd_button"
></Preference>
...
and pref_reset_bd_button.xml in layout folder
和pref_reset_bd_button。xml布局文件夹中
<?xml version="1.0" encoding="utf-8"?>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/resetButton"
android:text="@string/ajustes_almacenamiento_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="resetearBD">
</Button>
#4
5
Add
添加
setContentView(R.layout.buttonLayout);
Below
下面
addPreferencesFromResource(R.xml.yourPreference);
buttonLayout:
buttonLayout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/top_control_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<LinearLayout
android:id="@+id/bottom_control_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="@string/saveAlarm"/>
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_above="@id/bottom_control_bar"
android:layout_below="@id/top_control_bar"
android:choiceMode="multipleChoice">
</ListView>
Access Button by:
访问按钮:
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Your event
}
});
You can get the button on top or on bottom of the screen by putting the button in RelativeLayout:
你可以在屏幕的顶部或底部设置按钮,将按钮放在相对位置:
- top_control_bar
- top_control_bar
- bottom_control_bar
- bottom_control_bar
bottom_control_bar
bottom_control_bar
This worked for me. I hope I can help someone with this piece of code.
这为我工作。我希望我能帮助某人处理这段代码。
#5
1
I don't think there is an easy way to just add a Button to a preference screen. Preferences are limited to:
我不认为有一种简单的方法可以将一个按钮添加到偏好屏幕上。偏好是有限的:
EditTextPreference
ListPreference
RingtonePreference
CheckboxPreference
While using a preference screen, you are limited to the use of these options, and there are no single "button-preference" which could be easily used for your need.
在使用偏好屏幕时,您只能使用这些选项,而且没有一个简单的“button-preference”可以很容易地用于您的需要。
I've been looking for similar functionality, with adding buttons to the preference screen, but it seems like you need to build your own screen for this, managing the change to preferences in your own implementation.
我一直在寻找类似的功能,向首选项屏幕添加按钮,但似乎需要为此构建自己的屏幕,在自己的实现中管理对首选项的更改。
#6
1
You can do it like this to access the button!
你可以这样来访问这个按钮!
View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layoutButtons, null, false);
Don't forget to add android:id
to the LinearLayout that contains the button in layoutButtons.xml, i.e. android:id="@+id/mylayout"
不要忘记在线性布局中添加android:id,其中包含layoutButtons中的按钮。xml,即android:id =“@ + id / mylayout”
LinearLayout mLayout = (LinearLayout) footerView.findViewById(R.id.mylayout);
Button login = (Button) footerView.findViewById(R.id.loginButton);
login.setOnClickListener(this);
ListView lv = (ListView) findViewById(android.R.id.list);
lv.addFooterView(footerView);
// Lines 2 and 3 can be used in the same way for a second Button!
#7
0
try android:onClick="myMethod" works like a charm for simple onclick events
试试android:onClick=“myMethod”就像一个简单的onClick事件的咒语
#8
0
You can also customise the layout of a Preference by overriding Preference.onCreateView(parent)
. The example below uses an anonymous inner class to make red preferences.
您还可以通过重写Preference.onCreateView(父类)来定制首选项的布局。下面的示例使用一个匿名内部类来创建红色首选项。
screen.addPreference(
new Preference(context) {
@Override
protected View onCreateView(ViewGroup parent) {
View view = super.onCreateView(parent);
view.setBackgroundColor(Color.RED);
return view;
}
});
You could use this technique to add a button to the default view.
您可以使用此技术向默认视图添加一个按钮。
#1
254
For the xml:
xml:
<Preference android:title="Acts like a button"
android:key="@string/myCoolButton"
android:summary="This is a cool button"/>
Then for the java in your onCreate()
然后对于onCreate()中的java
Preference button = findPreference(getString(R.string.myCoolButton));
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
//code for what you want it to do
return true;
}
});
This will appear like a normal Preference, with just a title and a summary, so it will look like it belongs.
这将看起来像一个普通的首选项,只有一个标题和一个摘要,因此它看起来像是属于它的。
Edit: I changed to using @string/myCoolButton
and getString(R.string.myCoolButton)
because it's best to use resources for compiler assistance, language translation, and ease of String changes.
编辑:我改为使用@string/myCoolButton和getString(R.string.myCoolButton),因为它最好使用资源进行编译器辅助、语言转换和轻松的字符串更改。
#2
36
I wanted to add an Exit link to the preferences and was able to modify Jakar's code to make it work like this:
我想添加一个退出链接到首选项,并能够修改Jakar的代码使其工作如下:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Settings">
<Preference android:title="Click to exit" android:key="exitlink"/>
</PreferenceCategory>
</PreferenceScreen>
Originally the 'Preference' was a 'EditTextPreference' which I hand edited.
最初,“偏好”是“EditTextPreference”,是我亲手编辑的。
Then in the class:
然后在课堂上:
public class MyPreferences extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.mypreferences);
Preference button = (Preference)getPreferenceManager().findPreference("exitlink");
if (button != null) {
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference arg0) {
finish();
return true;
}
});
}
}
}
#3
33
I suppouse its too late. This is what i have done for a Button Preference.
我觉得太晚了。这就是我对按钮首选项所做的。
The preference in preference.xml file in xml folder
偏好的偏好。xml文件夹中的xml文件
....
<Preference
android:key="resetBD"
android:title="@string/ajustes_almacenamiento"
android:summary="@string/ajustes_almacenamiento_desc"
android:widgetLayout="@layout/pref_reset_bd_button"
></Preference>
...
and pref_reset_bd_button.xml in layout folder
和pref_reset_bd_button。xml布局文件夹中
<?xml version="1.0" encoding="utf-8"?>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/resetButton"
android:text="@string/ajustes_almacenamiento_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="resetearBD">
</Button>
#4
5
Add
添加
setContentView(R.layout.buttonLayout);
Below
下面
addPreferencesFromResource(R.xml.yourPreference);
buttonLayout:
buttonLayout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/top_control_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<LinearLayout
android:id="@+id/bottom_control_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="@string/saveAlarm"/>
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_above="@id/bottom_control_bar"
android:layout_below="@id/top_control_bar"
android:choiceMode="multipleChoice">
</ListView>
Access Button by:
访问按钮:
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Your event
}
});
You can get the button on top or on bottom of the screen by putting the button in RelativeLayout:
你可以在屏幕的顶部或底部设置按钮,将按钮放在相对位置:
- top_control_bar
- top_control_bar
- bottom_control_bar
- bottom_control_bar
bottom_control_bar
bottom_control_bar
This worked for me. I hope I can help someone with this piece of code.
这为我工作。我希望我能帮助某人处理这段代码。
#5
1
I don't think there is an easy way to just add a Button to a preference screen. Preferences are limited to:
我不认为有一种简单的方法可以将一个按钮添加到偏好屏幕上。偏好是有限的:
EditTextPreference
ListPreference
RingtonePreference
CheckboxPreference
While using a preference screen, you are limited to the use of these options, and there are no single "button-preference" which could be easily used for your need.
在使用偏好屏幕时,您只能使用这些选项,而且没有一个简单的“button-preference”可以很容易地用于您的需要。
I've been looking for similar functionality, with adding buttons to the preference screen, but it seems like you need to build your own screen for this, managing the change to preferences in your own implementation.
我一直在寻找类似的功能,向首选项屏幕添加按钮,但似乎需要为此构建自己的屏幕,在自己的实现中管理对首选项的更改。
#6
1
You can do it like this to access the button!
你可以这样来访问这个按钮!
View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layoutButtons, null, false);
Don't forget to add android:id
to the LinearLayout that contains the button in layoutButtons.xml, i.e. android:id="@+id/mylayout"
不要忘记在线性布局中添加android:id,其中包含layoutButtons中的按钮。xml,即android:id =“@ + id / mylayout”
LinearLayout mLayout = (LinearLayout) footerView.findViewById(R.id.mylayout);
Button login = (Button) footerView.findViewById(R.id.loginButton);
login.setOnClickListener(this);
ListView lv = (ListView) findViewById(android.R.id.list);
lv.addFooterView(footerView);
// Lines 2 and 3 can be used in the same way for a second Button!
#7
0
try android:onClick="myMethod" works like a charm for simple onclick events
试试android:onClick=“myMethod”就像一个简单的onClick事件的咒语
#8
0
You can also customise the layout of a Preference by overriding Preference.onCreateView(parent)
. The example below uses an anonymous inner class to make red preferences.
您还可以通过重写Preference.onCreateView(父类)来定制首选项的布局。下面的示例使用一个匿名内部类来创建红色首选项。
screen.addPreference(
new Preference(context) {
@Override
protected View onCreateView(ViewGroup parent) {
View view = super.onCreateView(parent);
view.setBackgroundColor(Color.RED);
return view;
}
});
You could use this technique to add a button to the default view.
您可以使用此技术向默认视图添加一个按钮。