自preferenceScreen与可单击按钮 [英] Custom PreferenceScreen with clickable buttons
本文介绍了自preferenceScreen与可单击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建以下的自定义preference屏幕。
我要添加监听器上Button1的和Button2的。我应该怎么办呢?
我使用下面的$ C $下创建上述preference屏幕。
演示preF:
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
公共类演示preF延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
startActivity(新意图(这一点,我的pref.class));
}
}
我的preF:
进口android.os.Bundle;
。进口的Android preference preferenceActivity;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
公共类我的preF扩展preferenceActivity {
@覆盖
公共无效的onCreate(包冰柱){
super.onCreate(冰柱);
//setContentView(R.layout.tftp_setting);
this.add preferencesFromResource(R.xml.list_ preF);
/ *按钮B1 =(按钮)findViewById(R.id.button1);
b1.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
Log.i(我的preF,Button1的一个点击。);
}
});
按钮B2 =(按钮)findViewById(R.id.button2);
b2.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
Log.i(我的preF,Button2的一个点击。);
}
}); * /
}
}
水库\布局\ Setting.xml的:
< XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直
>
< LinearLayout中的android:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:方向=横向>
< TextView的Android版本:文本=TextView1机器人:ID =@ + ID / textView1机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>< / TextView的>
< EditText上的android:文本=EditText2机器人:ID =@ + ID / editText1机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>< /的EditText>
< / LinearLayout中>
< LinearLayout中的android:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:方向=横向>
< TextView的Android版本:文本=TextView2机器人:ID =@ + ID / textView2机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>< / TextView的>
< EditText上的android:文本=EditText2机器人:ID =@ + ID / editText2机器人:layout_height =WRAP_CONTENT机器人:layout_width =WRAP_CONTENT>< /的EditText>
< / LinearLayout中>
< LinearLayout中的android:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:方向=横向>
<按钮的android:文本=Button1的机器人:ID =@ + ID / button1的机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>< /按钮>
<按钮的android:文本=Button2的机器人:ID =@ + ID /按钮2机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>< /按钮>
< / LinearLayout中>
< / LinearLayout中>
水库\ XML \ pref.xml:
< XML版本=1.0编码=UTF-8&GT?;
< preferenceScreen
的xmlns:机器人=http://schemas.android.com/apk/res/android>
< preferenceScreen机器人:标题=我的设置的Android:布局=@布局/设置机器人:汇总=这是我的设置。>< / preferenceScreen>
< / preferenceScreen>
解决方案
您需要:
按钮B1 =(按钮)findViewById(R.id.button1);
按钮B2 =(按钮)findViewById(R.id.button2);
b1.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
Log.i(我的preF,Button1的一个点击。);
}
});
b2.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
Log.i(我的preF,Button2的一个点击。);
}
});
I have created following Custom Preference Screen.
I want to add Listener on Button1 and Button2. How should I do it?
I am using following code for creating above Preference Screen.
DemoPref:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class DemoPref extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startActivity(new Intent(this, MyPref.class));
}
}
MyPref:
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MyPref extends PreferenceActivity{
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//setContentView(R.layout.tftp_setting);
this.addPreferencesFromResource(R.xml.list_pref);
/*Button b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Log.i("MyPref", "Button1 one is clicked.");
}
});
Button b2 = (Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Log.i("MyPref", "Button2 one is clicked.");
}
});*/
}
}
res\layout\setting.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:text="TextView1" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:text="EditText2" android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:text="TextView2" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:text="EditText2" android:id="@+id/editText2" android:layout_height="wrap_content" android:layout_width="wrap_content"></EditText>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:text="Button1" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Button2" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
res\xml\pref.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen android:title="My Setting" android:layout="@layout/setting" android:summary="This is my setting."></PreferenceScreen>
</PreferenceScreen>
解决方案
You need:
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("MyPref", "Button1 one is clicked.");
}
});
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("MyPref", "Button2 one is clicked.");
}
});
这篇关于自preferenceScreen与可单击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文