برنامه بنویسید که پس از دریافت ورودی از EditText انرا بازدن یک دکمه به listView انتقال دهد.
در اندروید
کد xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.amir.test6.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello User!" android:textSize="30dp" android:background="#af1" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintHorizontal_bias="0.802" app:layout_constraintVertical_bias="0.054" android:id="@+id/textView" android:layout_marginEnd="76dp" android:layout_alignParentTop="true" android:layout_alignParentEnd="true" /> <ListView android:layout_width="138dp" android:layout_height="495dp" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="8dp" android:id="@+id/listview" android:background="#8ad" android:layout_alignTop="@+id/additem" android:layout_alignParentStart="true" android:layout_marginTop="25dp" /> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="23dp" android:ems="10" android:inputType="textPersonName" android:hint="Please Enter Your Text" android:background="#dd5" android:layout_below="@+id/textView" android:layout_alignParentEnd="true" android:layout_marginEnd="37dp" /> <Button android:id="@+id/additem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="list add" android:hint="Please Enter Your Text" android:background="#f8f" android:="" android:layout_marginTop="24dp" android:layout_below="@+id/editText" android:layout_toEndOf="@+id/listview" /> </RelativeLayout>
کد activity :
public class MainActivity extends AppCompatActivity { EditText editText; Button addButton; ListView listView; ArrayList<String> listItems; ArrayAdapter<String> adapter; String []str ={"tehran","esfahan","shiraz","sistan","kerman","khorasan"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.editText); addButton = (Button) findViewById(R.id.additem); listView = (ListView) findViewById(R.id.listview); listItems = new ArrayList<String>(); listItems.add("tehran"); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems); listView.setAdapter(adapter); addButton.setListener(new View.Listener() { public void (View v) { listItems.add(editText.getText().toString()); adapter.notifyDataSetChanged(); } }); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> a, View v, int position, long id) { Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_LONG).show(); } }); } }