کار با TextView و EditTextو ButtonوListView در اندروید
يكشنبه, ۲ ارديبهشت ۱۳۹۷، ۰۲:۵۷ ب.ظ
برنامه ای بنویسید که از textview نوع عملیات ریاصی را دریافت کرده و در اکتیویتی دوم با 2 عدد EditText مقدار بگیرد و با زدن دکمه عملیات مورد کلیک را انحام دهد.
منطق برنامه : از یک منوی تکس ویو میتوان از ویوهای ایجاد شده یک عملیات انتخاب و در اکتیویتی دوم مورد اجرا قرار گیرد
کد xml اکتیویتی اول :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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.test4.MainActivity">
<TextView
android:layout_width="wrap_content"
android:id="@+id/hal"
android:layout_height="wrap_content"
android:text="ittraining.blog.ir"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="#9999ff"
app:layout_constraintHorizontal_bias="0.908"
app:layout_constraintVertical_bias="0.206" />
<ListView
android:id="@+id/list_view"
android:layout_width="138dp"
android:layout_height="370dp"
tools:layout_editor_absoluteX="-3dp"
tools:layout_editor_absoluteY="-5dp"
/>
</android.support.constraint.ConstraintLayout>
کد اکتیویتی اول:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_view);
text = (TextView)findViewById(R.id.hal) ;
strings = new String[] {"sum","zarb","taghsim","tafrigh","jazr"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2,android.R.id.text1,strings);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String str = strings [position].toString();
Toast.makeText(getBaseContext(),str, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,act2.class);
intent.putExtra(ACTION,str);
startActivity(intent);
});
}
}
کد xml اکتیویتی دوم :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="@+id/l1"
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.test4.act2"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="87dp"
android:background="#99ff55"
android:onClick="OnClick"
android:text="show res"
app:layout_constraintHorizontal_bias="0.028"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="47dp"
android:background="#faa"
android:ems="10"
android:hint="value 2"
android:inputType="number"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="143dp"
android:layout_height="139dp"
android:background="#abc"
android:textColor="#000"
android:textSize="50px"
android:text="TextView"
android:layout_marginLeft="238dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="98dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:background="#ff0"
android:ems="10"
android:hint="value 1"
android:inputType="number"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
کد اکتیویتی دوم :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act2);
editText1 = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
tv = (TextView) findViewById(R.id.textView2);
constraintLayout = (ConstraintLayout) findViewById(R.id.l1);
Action = getIntent().getStringExtra(MainActivity.ACTION);
}
public void OnClick (View view)
{
if(Action.equals("sum"))
{
int j = Integer.parseInt(editText1.getText().toString()) + Integer.parseInt(editText2.getText().toString());
tv.setText(Integer.toString(j));
}
if (Action.equals("zarb"))
{
int j = Integer.parseInt(editText1.getText().toString()) * Integer.parseInt(editText2.getText().toString());
tv.setText(Integer.toString(j));
}
if (Action.equals("taghsim"))
{
int j = Integer.parseInt(editText1.getText().toString()) / Integer.parseInt(editText2.getText().toString());
tv.setText(Integer.toString(j));
}
if (Action.equals("tafrigh"))
{
int j = Integer.parseInt(editText1.getText().toString()) - Integer.parseInt(editText2.getText().toString());
tv.setText(Integer.toString(j));
}
if (Action.equals("jazr"))
{
double i = Double.parseDouble(editText1.getText().toString()) ;
float j = (float)Math.sqrt(i);
tv.setText(String.valueOf(j));
}
}