是否有可能写出类似的结构?
我想以某种方式设置类型T的参数的默认值.
private T GetNumericVal<T>(string sColName, T defVal = 0)
{
string sVal = GetStrVal(sColName);
T nRes;
if (!T.TryParse(sVal, out nRes))
return defVal;
return nRes;
}
Run Code Online (Sandbox Code Playgroud)
另外,我发现以下链接:
通用类型转换FROM字符串
我认为,此代码必须工作
private T GetNumericVal<T>(string sColName, T defVal = default(T)) where T : IConvertible
{
string sVal = GetStrVal(sColName);
try
{
return (T)Convert.ChangeType(sVal, typeof(T));
}
catch (FormatException)
{
return defVal;
}
}
Run Code Online (Sandbox Code Playgroud) 我可以通过android:layout
属性为首选项设置适当的布局.举个例子
<Preference
android:key="friction"
android:title="@string/friction"
android:layout="@layout/friction_fragment"
android:shouldDisableView="true"
android:defaultValue="30"
android:enabled="true"
android:selectable="true"
android:summary="Bite friction">
</Preference>
Run Code Online (Sandbox Code Playgroud)
布局在哪里
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:text="@string/friction" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"></TextView>
<SeekBar android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/sbFriction"></SeekBar>
<TextView android:text="@string/friction_little" android:id="@+id/txtSummary" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<Button android:text="Button" android:id="@+id/btnFriction" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我可以在PreferenceActivity中获取OnCreate中的视图
Preference fric = (Preference)this.findPreference("friction");
View v = fric.getView(null, null);
SeekBar sbFriction = (SeekBar)v.findViewById(R.id.sbFriction);
sbFriction.setOnSeekBarChangeListener(this);
Button btnFric = (Button) v.findViewById(R.id.btnFriction);
btnFric.setOnClickListener(m_onClick);
Run Code Online (Sandbox Code Playgroud)
但是我设定的这些事件听众并没有被解雇.我如何捕获这些事件,例如 - 单击按钮.编辑.不,它没有发动任何异常.这是更详细的代码
public class SettingsActivity extends PreferenceActivity implements OnPreferenceChangeListener, OnSeekBarChangeListener
{
private TextView …
Run Code Online (Sandbox Code Playgroud)