2016-05-10 1 views
0

Entwickeln Sie eine Android-App hier bin ich dynamisch generieren EditText und Schaltfläche wie Call und Remove von row.xml-Datei. Hinzufügen dieser Steuerelemente zu LinearLayout Ich implementiert, wie ich möchte, aber mein Problem ist, dass ich die EditText Werte in XML-Datei speichern möchte. Damit das nächste Mal, wenn ich diese App öffne, werden die bestimmten Werte aus der XML-Datei aufgefüllt.Nicht in der Lage, LinearLayout Werte in XML-Datei zu speichern

enter image description here

Hinzufügen von Namen und Handynummer TextBox und klicken Sie auf "Add" Schaltfläche diese Werte zu LinearLayout dynamisch hinzugefügt werden. Wenn ich auf "Add" klicke, werden diese Werte in der XML-Datei gespeichert und beim nächsten Start der App werden die gespeicherten XML-Werte in LinearLayout eingetragen.

dies ist mein Code

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> 

Antwort

1

Nein, Sie können nicht

Sie können nicht speichern Kontrollen dynamisch in Ihre xml Layout. Entweder Sie es manuell in xml hinzufügen oder Sie können separate xml zur Steuerung erstellen und dann include es xml in Ihre Mutter xml

<include 
    android:layout="id of your layout" 
//height ... width 
/> 

In beiden Richtungen, müssen Sie xml erstellen.

Wir erstellen xml und blasen es in unserer java Klasse auf. Was Sie tun wollen, ist völlig umgekehrt.

aktualisieren

Verwendung gemeinsamen Vorliebe

SharedPreference shared= context.getSharedPreferences("your preference name", 
       Context.MODE_PRIVATE); 

String value = shared.getString("UserPhone","no data"); 

//set this value to your control 
+0

Ich speichere das Steuerelement dynamisch Ich möchte nur die Werte speichern. – Pritish

+0

Speichern Sie es in einer gemeinsamen Einstellung, und wenn Sie Ihre dynamische Ansicht erstellen, legen Sie den alten Wert aus den gemeinsamen Einstellungen fest. – KDeogharkar

+0

Hier speichere ich im freigegebenen Einstellungsordner edit.PutString ("UserName", textIn.Text.Trim()); edit.PutString ("UserPhone", txtPhoneNo.Text.Trim()); bearbeiten.Sich bewerben(); mit diesem Code aber, wie ich es beim nächsten Öffnen der App beim nächsten Mal – Pritish

0

Ich glaube, Sie SharedPreferences in Android verweisen müssen. Dies wird Ihrem Zweck dienen.

+0

haben Sie eine Probe haben .... – Pritish

+0

http://www.101apps.co.za/index.php/articles/ using-androids-sharedpreferences-to-save-data.html .. Beispiel – Sush

+0

Kann ich mehr als einen Wert in SharedPreferences wie List-Objekt – Pritish

Verwandte Themen