无法将 LinearLayout 值保存到 xml 文件
Not able to save LinearLayout values to xml file
在这里开发一个 android 应用程序 我正在从 row.xml 文件动态生成 EditText
和 Call
和 Remove
等按钮。将这些控件添加到 LinearLayout
我按照自己的意愿实现,但我的问题是我想将 EditText
值保存到 xml 文件中。这样下次我打开这个应用程序时,特定的值就会从 xml 文件中填充。
将姓名和手机号码添加到文本框并单击 "Add"
按钮,这些值将动态添加到 LinearLayout
。当我点击添加时,这个值被存储到 xml 文件中,每当我下次启动应用程序时,存储的 xml 值被填充到 LinearLayout
.
这是我的代码
public class MainActivity : Activity
{
int count = 1;
EditText textIn, txtPhoneNo;
Button buttonAdd;
LinearLayout container;
EditText textOut;
System.Collections.ArrayList arrList = new System.Collections.ArrayList();
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
textIn = (EditText)FindViewById(Resource.Id.textin);
txtPhoneNo = (EditText)FindViewById(Resource.Id.txtPhoneNo);
buttonAdd = (Button)FindViewById(Resource.Id.add);
container = (LinearLayout)FindViewById(Resource.Id.container);
}
private void buttonAdd_Click(object sender, EventArgs e)
{
LayoutInflater layoutInflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
View addView = layoutInflater.Inflate(Resource.Layout.row, null);
textOut = (EditText)addView.FindViewById(Resource.Id.textout);
arrList.Add(txtPhoneNo.Text);
if (textIn.Text != "" && txtPhoneNo.Text != "")
{
textOut.SetText(textIn.Text + " : " + txtPhoneNo.Text, TextView.BufferType.Normal);
container.AddView(addView);
Button btnCall = (Button)addView.FindViewById(Resource.Id.btnCall);
btnCall.Click += BtnCall_Click;
Button buttonRemove = (Button)addView.FindViewById(Resource.Id.remove);
buttonRemove.Click += ButtonRemove_Click;
}
else
{
Toast.MakeText(this, "Field can not be blank.", ToastLength.Short).Show();
}
}
private void BtnCall_Click(object sender, EventArgs e)
{
var callDialog = new AlertDialog.Builder(this);
string strNo = After(textOut.Text,":");
callDialog.SetMessage("Call " + strNo + "?");
callDialog.SetNeutralButton("Call", delegate
{
var callIntent = new Intent(Intent.ActionCall);
callIntent.SetData(Android.Net.Uri.Parse("tel:" + strNo));
StartActivity(callIntent);
});
callDialog.SetNegativeButton("Cancel", delegate { });
// Show the alert dialog to the user and wait for response.
callDialog.Show();
}
}
}
Main.axml
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/textin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:hint="name" />
<EditText
android:id="@+id/txtPhoneNo"
android:layout_width="345.0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:hint="Phone No." />
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Add" />
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>
row.xml
<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="wrap_content">
<Button
android:id="@+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Remove"/>
<Button
android:id="@+id/btnCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/remove"
android:text="Call"/>
<EditText
android:id="@+id/textout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/remove"/>
</RelativeLayout>
我认为你需要参考 android 中的 SharedPreferences
。这将达到您的目的。
不,你不能
您不能将控件动态存储到 xml
布局中。
您可以手动将其添加到 xml
中,也可以创建单独的 xml
进行控制,然后 include
将其 xml
添加到您的父 xml
中
<include
android:layout="id of your layout"
//height ... width
/>
在这两种方式中,您都必须创建 xml
。
我们正在创建 xml
并在我们的 java
class 中对其进行膨胀。你想做的是完全相反的。
更新
使用共享首选项
SharedPreference shared= context.getSharedPreferences("your preference name",
Context.MODE_PRIVATE);
String value = shared.getString("UserPhone","no data");
//set this value to your control
在这里开发一个 android 应用程序 我正在从 row.xml 文件动态生成 EditText
和 Call
和 Remove
等按钮。将这些控件添加到 LinearLayout
我按照自己的意愿实现,但我的问题是我想将 EditText
值保存到 xml 文件中。这样下次我打开这个应用程序时,特定的值就会从 xml 文件中填充。
将姓名和手机号码添加到文本框并单击 "Add"
按钮,这些值将动态添加到 LinearLayout
。当我点击添加时,这个值被存储到 xml 文件中,每当我下次启动应用程序时,存储的 xml 值被填充到 LinearLayout
.
这是我的代码
public class MainActivity : Activity
{
int count = 1;
EditText textIn, txtPhoneNo;
Button buttonAdd;
LinearLayout container;
EditText textOut;
System.Collections.ArrayList arrList = new System.Collections.ArrayList();
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
textIn = (EditText)FindViewById(Resource.Id.textin);
txtPhoneNo = (EditText)FindViewById(Resource.Id.txtPhoneNo);
buttonAdd = (Button)FindViewById(Resource.Id.add);
container = (LinearLayout)FindViewById(Resource.Id.container);
}
private void buttonAdd_Click(object sender, EventArgs e)
{
LayoutInflater layoutInflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
View addView = layoutInflater.Inflate(Resource.Layout.row, null);
textOut = (EditText)addView.FindViewById(Resource.Id.textout);
arrList.Add(txtPhoneNo.Text);
if (textIn.Text != "" && txtPhoneNo.Text != "")
{
textOut.SetText(textIn.Text + " : " + txtPhoneNo.Text, TextView.BufferType.Normal);
container.AddView(addView);
Button btnCall = (Button)addView.FindViewById(Resource.Id.btnCall);
btnCall.Click += BtnCall_Click;
Button buttonRemove = (Button)addView.FindViewById(Resource.Id.remove);
buttonRemove.Click += ButtonRemove_Click;
}
else
{
Toast.MakeText(this, "Field can not be blank.", ToastLength.Short).Show();
}
}
private void BtnCall_Click(object sender, EventArgs e)
{
var callDialog = new AlertDialog.Builder(this);
string strNo = After(textOut.Text,":");
callDialog.SetMessage("Call " + strNo + "?");
callDialog.SetNeutralButton("Call", delegate
{
var callIntent = new Intent(Intent.ActionCall);
callIntent.SetData(Android.Net.Uri.Parse("tel:" + strNo));
StartActivity(callIntent);
});
callDialog.SetNegativeButton("Cancel", delegate { });
// Show the alert dialog to the user and wait for response.
callDialog.Show();
}
}
}
Main.axml
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/textin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:hint="name" />
<EditText
android:id="@+id/txtPhoneNo"
android:layout_width="345.0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:hint="Phone No." />
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Add" />
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>
row.xml
<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="wrap_content">
<Button
android:id="@+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Remove"/>
<Button
android:id="@+id/btnCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/remove"
android:text="Call"/>
<EditText
android:id="@+id/textout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/remove"/>
</RelativeLayout>
我认为你需要参考 android 中的 SharedPreferences
。这将达到您的目的。
不,你不能
您不能将控件动态存储到 xml
布局中。
您可以手动将其添加到 xml
中,也可以创建单独的 xml
进行控制,然后 include
将其 xml
添加到您的父 xml
<include
android:layout="id of your layout"
//height ... width
/>
在这两种方式中,您都必须创建 xml
。
我们正在创建 xml
并在我们的 java
class 中对其进行膨胀。你想做的是完全相反的。
更新
使用共享首选项
SharedPreference shared= context.getSharedPreferences("your preference name",
Context.MODE_PRIVATE);
String value = shared.getString("UserPhone","no data");
//set this value to your control