我正在Button Click上动态创建LinearLayout(在另一个包含两个静态EditText和ImageView的布局中),包含EditText和ImageView。但是当应用程序启动时它什么都不会显示那么如何保留LinearLayout。我试过这个。在此先感谢。
public class HelpAlertActivity extends AppCompatActivity implements View.OnClickListener {
public SharedPreferences sharedpreference;
LinearLayout add_new_layout, mainLayout, image_btn_layout, buttonLayout;
FrameLayout frameLayout;
Button btn_one, bt_submit;
MyEditText et_contact_one, et_contact_two, etAdd;
ImageView image_one, image_two;
ImageButton imageButton;
FloatingActionButton fab;
SharedPreferences.Editor editor;
List<MyEditText> allEds;
List<ImageView> allImg;
ImageView imageView;
String contactOne, contactTwo, number1, number2, number3, number4, number5, number6, number7, number8, number9, number10, number11, number12, number13;
StringBuilder stringBuilder;
private int counter = 0,many=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help_alert);
initialization();
}
private void initialization() {
sharedpreference = getSharedPreferences("preference", Context.MODE_APPEND);
editor = sharedpreference.edit();
allEds = new ArrayList<>();
allImg = new ArrayList<>();
mainLayout = (LinearLayout) findViewById(R.id.main_layout);
image_btn_layout = (LinearLayout) findViewById(R.id.image_btn_layout);
buttonLayout = (LinearLayout) findViewById(R.id.buttonLayout);
add_new_layout = (LinearLayout) findViewById(R.id.newLayout);
fab = (FloatingActionButton) findViewById(R.id.fab);
et_contact_one = (MyEditText) findViewById(R.id.contact_one);
et_contact_two = (MyEditText) findViewById(R.id.contact_two);
image_one = (ImageView) findViewById(R.id.image_one);
image_two = (ImageView) findViewById(R.id.image_two);
btn_one = (Button) findViewById(R.id.btn_one);
bt_submit = (Button) findViewById(R.id.bt_submit);
fab.setOnClickListener(this);
image_one.setOnClickListener(this);
image_two.setOnClickListener(this);
btn_one.setOnClickListener(this);
bt_submit.setOnClickListener(this);
stringBuilder = new StringBuilder();
if (sharedpreference != null) {
fab.setVisibility(View.VISIBLE);
buttonLayout.setVisibility(View.INVISIBLE);
image_btn_layout.setVisibility(View.INVISIBLE);
} else {
buttonLayout.setVisibility(View.VISIBLE);
image_btn_layout.setVisibility(View.VISIBLE);
fab.setVisibility(View.INVISIBLE);
}
int no_of_layouts = sharedpreference.getInt("no_of_layouts",0);
while(no_of_layouts>0){
LinearLayout parentView = (LinearLayout) this.findViewById(R.id.main_layout);
LinearLayout childView = new LinearLayout(getApplicationContext());
etAdd = generateEditText(counter);
//add linear layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(45, 45);
layoutParams.setMargins(0, 25, 20, 0);
imageView = new ImageView(this);
imageView.setBackgroundResource(R.drawable.add_contact);
// imageView.setId(counter + 3);
imageView.setAdjustViewBounds(true);
imageView.setLayoutParams(layoutParams);
getValue(imageView);
allEds.add(etAdd);
allImg.add(imageView);
childView.addView(etAdd);
childView.addView(imageView);
parentView.addView(childView);
no_of_layouts--;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_one:
if (this.counter < 10) {
LinearLayout parentView = (LinearLayout) this.findViewById(R.id.main_layout);
LinearLayout childView = new LinearLayout(getApplicationContext());
etAdd = generateEditText(counter);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(45, 45);
layoutParams.setMargins(0, 25, 20, 0);
imageView = new ImageView(this);
imageView.setBackgroundResource(R.drawable.add_contact);
imageView.setId(counter + 3);
imageView.setAdjustViewBounds(true);
imageView.setLayoutParams(layoutParams);
getValue(imageView);
allEds.add(etAdd);
allImg.add(imageView);
childView.addView(etAdd);
childView.addView(imageView);
parentView.addView(childView);
counter++;
many++;
editor.putInt("no_of_layouts", many);
} else {
// v.setEnabled(false);
}
break;
case R.id.image_one:
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 1);
break;
case R.id.image_two:
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 2);
break;
case 3:
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 3);
break;
case 4:
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 4);
break;
case 5:
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 5);
break;
case 6:
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 6);
break;
case 7:
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 7);
break;
case 8:
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 8);
break;
case 9:
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 9);
break;
case 10:
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 10);
break;
case 11:
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 11);
break;
case 12:
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 12);
break;
case 13:
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 13);
break;
case R.id.bt_submit:
String ctone = et_contact_one.getText().toString();
String ct_two = et_contact_two.getText().toString();
int size = allEds.size();
String[] strings = new String[size];
for (int i = 0; i < allEds.size(); i++) {
strings[i] = allEds.get(i).getText().toString();
editor.putString("contact" + i, strings[i]);
Log.e("Added number", "" + i);
// Toast.makeText(this, "contact" + i, Toast.LENGTH_SHORT).show();
}
if (ctone.equals("") && ctone.isEmpty() || ct_two.equals("") && ct_two.isEmpty()) {
Toast.makeText(this, "Please enter mobile", Toast.LENGTH_SHORT).show();
} else {
editor.putString("contactOne", ctone);
editor.putString("contactTwo", ct_two);
editor.commit();
Toast.makeText(this, "Contact Saved ", Toast.LENGTH_SHORT).show();
startActivity(new Intent(this, MainActivity.class));
// }
break;
}
case R.id.fab:
buttonLayout.setVisibility(View.VISIBLE);
image_btn_layout.setVisibility(View.VISIBLE);
fab.setVisibility(View.INVISIBLE);
break;
}
}
private void getValue(ImageView imageView) {
imageView.setOnClickListener(this);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case 1:
if (resultCode == RESULT_OK) {
String num = GetContact.getdetails(data, getApplicationContext());
et_contact_one.setText(num);
}
break;
case 2:
if (resultCode == RESULT_OK) {
number2 = GetContact.getdetails(data, getApplicationContext());
et_contact_two.setText(number2.replaceAll("[- ]",""));
}
break;
case 3:
if (resultCode == RESULT_OK ) {
number3 = GetContact.getdetails(data, getApplicationContext());
etAdd.setText(number3.replaceAll("[- ]",""));
}
break;
case 4:
if (resultCode == RESULT_OK) {
number4 = GetContact.getdetails(data, getApplicationContext());
etAdd.setText(number4.replaceAll("[- ]",""));
}
break;
case 5:
if (resultCode == RESULT_OK) {
number5 = GetContact.getdetails(data, getApplicationContext());
etAdd.setText(number5.replaceAll("[- ]",""));
}
break;
case 6:
if (resultCode == RESULT_OK) {
number6 = GetContact.getdetails(data, getApplicationContext());
etAdd.setText(number6.replaceAll("[- ]",""));
}
break;
case 7:
if (resultCode == RESULT_OK) {
number7 = GetContact.getdetails(data, getApplicationContext());
etAdd.setText(number7.replaceAll("[- ]",""));
}
break;
case 8:
if (resultCode == RESULT_OK) {
number8 = GetContact.getdetails(data, getApplicationContext());
etAdd.setText(number8.replaceAll("[- ]",""));
}
break;
case 9:
if (resultCode == RESULT_OK) {
number9 = GetContact.getdetails(data, getApplicationContext());
etAdd.setText(number9.replaceAll("[- ]",""));
}
break;
case 10:
if (resultCode == RESULT_OK) {
number10 = GetContact.getdetails(data, getApplicationContext());
etAdd.setText(number10.replaceAll("[- ]",""));
}
break;
case 11:
if (resultCode == RESULT_OK ) {
number11 = GetContact.getdetails(data, getApplicationContext());
etAdd.setText(number11.replaceAll("[- ]",""));
}
break;
case 12:
if (resultCode == RESULT_OK) {
number12 = GetContact.getdetails(data, getApplicationContext());
etAdd.setText(number12.replaceAll("[- ]",""));
}
break;
case 13:
if (resultCode == RESULT_OK) {
number13 = GetContact.getdetails(data, getApplicationContext());
etAdd.setText(number13.replaceAll("[- ]",""));
}
break;
}
}
public MyEditText generateEditText(int id) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, 90, 1.0f);
layoutParams.setMargins(15, 10, 0, 0);
MyEditText et = new MyEditText(this);
et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(13)});
et.setBackgroundResource(R.drawable.login_textfield_background);
et.setHint("Enter or Select number");
et.setInputType(InputType.TYPE_CLASS_NUMBER);
et.setPadding(35, 0, 30, 0);
et.setId(id);
et.setLayoutParams(layoutParams);
return et;
}
@Override
protected void onPause() {
super.onPause();
contactOne = sharedpreference.getString("contactOne", null);
contactTwo = sharedpreference.getString("contactTwo", null);
for (int i = 0; i < allEds.size(); i++) {
stringBuilder.append(sharedpreference.getString("contact" + i, "")+",");
}
}
@Override
protected void onResume() {
super.onResume();
et_contact_one.setText(contactOne);
et_contact_two.setText(contactTwo);
int size = allEds.size();
String[] strings = new String[size];
String[] lines = stringBuilder.toString().split(",");
for(String s : lines) {
// etAdd.setText(s);
/* strings[i] = allEds.get(i).getText().toString();
sharedpreference.getString("contact" + i, "");*/
}
}
/* public ImageView generateImageView(int id) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(45, 45);
layoutParams.setMargins(0, 25, 20, 0);
ImageView imageView = new ImageView(this);
imageView.setBackgroundResource(R.drawable.add_contact);
imageView.setId(id);
imageView.setAdjustViewBounds(true);
imageView.setLayoutParams(layoutParams);
return imageView;
}*/
}
activity_help_alert.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<a7colors.com.helpalert.CustomWidget.MyEditText
android:id="@+id/contact_one"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_weight="1.0"
android:maxLength="13"
android:freezesText="true"
android:background="@drawable/login_textfield_background"
android:hint="@string/contact_hint"
android:inputType="number"
android:paddingLeft="30dp"
android:paddingRight="30dp"
tools:ignore="ContentDescription"
/>
<ImageView
android:id="@+id/image_one"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="35dp"
android:adjustViewBounds="true"
android:background="@drawable/add_contact"
tools:ignore="ContentDescription"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<a7colors.com.helpalert.CustomWidget.MyEditText
android:id="@+id/contact_two"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1.0"
android:maxLength="13"
android:freezesText="true"
android:background="@drawable/login_textfield_background"
android:hint="@string/contact_hint"
android:inputType="number"
android:paddingLeft="30dp"
android:paddingRight="30dp" />
<ImageView
android:id="@+id/image_two"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="25dp"
android:adjustViewBounds="true"
android:background="@drawable/add_contact"
tools:ignore="ContentDescription"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/main_layout"
android:orientation="vertical"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/image_btn_layout"
android:orientation="vertical">
<Button
android:id="@+id/btn_one"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:background="@drawable/add_circular_button"
tools:ignore="ContentDescription" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:gravity="center"
>
<a7colors.com.helpalert.CustomWidget.MyButton
android:id="@+id/bt_submit"
android:layout_width="wrap_content"
android:layout_height="46dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/login_bt_background"
android:text="@string/btn_submit"
android:layout_marginStart="20dp"
android:layout_marginEnd="30dp"
android:textColor="@color/white" />
</LinearLayout>
</LinearLayout>
</ScrollView>
答案 0 :(得分:0)
当你创建了如下所示的布局时,你应该在SharedPreference
中保存一个变量。
case R.id.btn_one:
if (this.counter < 10) {
LinearLayout parentView = (LinearLayout) this.findViewById(R.id.main_layout);
LinearLayout childView = new LinearLayout(getApplicationContext());
etAdd = generateEditText(counter);
//add linear layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(45, 45);
layoutParams.setMargins(0, 25, 20, 0);
imageView = new ImageView(this);
imageView.setBackgroundResource(R.drawable.add_contact);
imageView.setId(counter + 3);
imageView.setAdjustViewBounds(true);
imageView.setLayoutParams(layoutParams);
getValue(imageView);
allEds.add(etAdd);
allImg.add(imageView);
childView.addView(etAdd);
childView.addView(imageView);
parentView.addView(childView);
counter++;
many++;
editor.putInt("no_of_layouts", many);
} else {
v.setEnabled(false);
}
on活动初始化检查此变量是否存在,如果为true,则创建如下所示的布局:
private void initialization() {
sharedpreference = getSharedPreferences("preference",
Context.MODE_APPEND);
editor = sharedpreference.edit();
allEds = new ArrayList<>();
allImg = new ArrayList<>();
mainLayout = (LinearLayout) findViewById(R.id.main_layout);
add_new_layout = (LinearLayout) findViewById(R.id.newLayout);
et_contact_one = (MyEditText) findViewById(R.id.contact_one);
et_contact_two = (MyEditText) findViewById(R.id.contact_two);
image_one = (ImageView) findViewById(R.id.image_one);
image_two = (ImageView) findViewById(R.id.image_two);
btn_one = (Button) findViewById(R.id.btn_one);
bt_submit = (Button) findViewById(R.id.bt_submit);
image_one.setOnClickListener(this);
image_two.setOnClickListener(this);
btn_one.setOnClickListener(this);
bt_submit.setOnClickListener(this);
// adding layout if created
int no_of_layouts = sharedpreference.getInt("no_of_layouts",0);
while(no_of_layouts>0){
// add whatever layout you have added before in onclick
no_of_layout--;
}
}
PS:我假设SharedPreference并且点击逻辑工作正常。 如果您遇到任何问题,请告诉我。