Son Forum Aktiviteleri
6KAYNAK KOD ELDE ETME
Uygulamamın kodunu kaybettim.Uygulamayı android studio ile telefonumda çalıştırmıştım.Uygulama hala telefonun anasayfasında duruyor.Apkden kod çekeN showjava uygulamasını denedim ama bu uygulama gözükmüyor.Kodları çekmenin bir yolu var mı ?
.
Operatör Gösterme
Operatörleri resimdeki gibi sayıların yanına yazdırmak istiyorum.Nasıl yapabilirim? Şimdiden teşekkürler :)
MainActivity.java
package com.example.tckcakar.myapplication;
import android.content.Context;
import android.content.SharedPreferences;
import android.provider.SyncStateContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
GridView gridView;
float ilkdeger = 0;
public double sonuc = 0;
public String islem;
EditText edit;
String displaystr;
String[] dizi = {
"7", "8", "9", "C", "4", "5", "6", "/", "1", "2", "3", "*", "0", "=", "+", "-"
};
public MainActivity() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit= (EditText) findViewById(R.id.edit);
gridView = (GridView) findViewById(R.id.gridView1);
ArrayAdapter<String> adapter = new MyAdapter(this);
adapter.addAll(dizi);//diziyi adaptöre gönderdik.
gridView.setAdapter(adapter);//gridview ve adaptörü bağladık.
adapter.notifyDataSetChanged();
edit.setEnabled(false);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.button:
TextView textView = (TextView) view;
String btnStr = textView.getText().toString();
kay(btnStr);
break;
}
}
public void kay (String btnStr) {
if (btnStr.equals("1")) {
edit.setText(edit.getText() +"1");
} else if (btnStr.equals("2")) {
edit.setText(edit.getText() +"2");
} else if (btnStr.equals("3")) {
edit.setText(edit.getText() +"3");
} else if (btnStr.equals("4")) {
edit.setText(edit.getText() +"4");
} else if (btnStr.equals("5")) {
edit.setText(edit.getText() +"5");
} else if (btnStr.equals("6")) {
edit.setText(edit.getText() +"6");
} else if (btnStr.equals("7")) {
edit.setText(edit.getText() +"7");
} else if (btnStr.equals("8")) {
edit.setText(edit.getText() +"8");
} else if (btnStr.equals("9")) {
edit.setText(edit.getText() +"9");
} else if (btnStr.equals("C")) {
try {
ilkdeger = 0;
sonuc = 0;
edit.setText("");
} catch (Exception C) {
}
} else if (btnStr.equals("+")) {
try {
if (!edit.getText().toString().isEmpty()) {
ilkdeger = Float.parseFloat(edit.getText().toString());
edit.setText("");
islem = "Toplama";
displaystr += "+";
}
} catch (Exception C) {
}
} else if (btnStr.equals("-")) {
try {
if (!edit.getText().toString().isEmpty()) {
ilkdeger = Float.parseFloat(edit.getText().toString());
edit.setText("");
islem = "Çıkarma";
}
} catch (Exception C) {
}
} else if (btnStr.equals("/")) {
try {
if (!edit.getText().toString().isEmpty()) {
ilkdeger = Float.parseFloat(edit.getText().toString());
edit.setText("");
islem = "Bölme";
}
} catch (Exception C) {
}
} else if (btnStr.equals("*")) {
try {
if (!edit.getText().toString().isEmpty()) {
ilkdeger = Float.parseFloat(edit.getText().toString());
edit.setText("");
islem = "Çarpma";
}
} catch (Exception C) {
}
} else if (btnStr.equals("="))
if (!edit.getText().toString().isEmpty() && !String.valueOf(ilkdeger).equals("0")) {
if (islem.equals("Toplama")) {
sonuc = ilkdeger + Float.parseFloat(edit.getText().toString());
} else if (islem.equals("Çıkarma")) {
sonuc = ilkdeger - Float.parseFloat(edit.getText().toString());
} else if (islem.equals("Çarpma")) {
sonuc = ilkdeger * Float.parseFloat(edit.getText().toString());
} else if (islem.equals("Bölme")) {
sonuc = ilkdeger / Float.parseFloat(edit.getText().toString());
}
edit.setText(String.valueOf(sonuc));
ilkdeger = 0;
sonuc = 0;
} else {
edit.setText("Değer giriniz");
}
if (R.id.button == gridView.getId()
) {
ilkdeger = 0;
sonuc = 0;
edit.setText("");
}
}
private class MyAdapter extends ArrayAdapter<String> {//adaptörü sınıf olarak oluşturduk.
MyAdapter(@NonNull Context context) {
super(context, android.R.layout.simple_list_item_1);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {// pozisyonunu alır ve çizer
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.calc_button, parent, false);//VİEWVİ GÖRSEL HALE GETİRRİ INFLATER
}
TextView button = convertView.findViewById(R.id.button);
button.setText(getItem(position));//pozisyonu alır.
button.setOnClickListener(MainActivity.this);
return convertView;
}
}}
Uygulama sürekli duruyor
Uygulama birkaç kez çalıştı daha sonra sürekli durmaya başladı ve aşağıdaki hatayı aldım.Clean ve rebulti denedim ama düzelmedi.
Logcat
08-08 10:18:14.484 30997-30997/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tckcakar.myapplication, PID: 30997
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
at com.example.tckcakar.myapplication.MainActivity$MyAdapter.getView(MainActivity.java:72)
at android.widget.AbsListView.obtainView(AbsListView.java:3238)
at android.widget.GridView.onMeasure(GridView.java:1096)
at android.view.View.measure(View.java:21051)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:21051)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6459)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:21051)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6459)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:21051)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6459)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:21051)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6459)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:21051)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6459)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:853)
at android.view.View.measure(View.java:21051)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2583)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1642)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1893)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1522)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7098)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
at android.view.Choreographer.doCallbacks(Choreographer.java:702)
at android.view.Choreographer.doFrame(Choreographer.java:638)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
MainActivity.java
package com.example.tckcakar.myapplication;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
GridView gridView;
float ilkdeger = 0;
public double sonuc = 0;
public String islem;
String[] dizi = {
"7", "8", "9", "C", "4", "5", "6", "/", "1", "2", "3", "*", "0", "=", "+", "-"
};
public MainActivity() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView1);
ArrayAdapter<String> adapter = new MyAdapter(this);
adapter.addAll(dizi);//diziyi adaptöre gönderdik.
gridView.setAdapter(adapter);//gridview ve adaptörü bağladık.
adapter.notifyDataSetChanged();
Intent intent=new Intent(this,click.class);
startActivity(intent);
}
public void onAttachFragment(Activity activity) {
}
@Override
public void onClick(View view) {
}
private class MyAdapter extends ArrayAdapter<String> {//adaptörü sınıf olarak oluşturduk.
MyAdapter(@NonNull Context context) {
super(context, android.R.layout.simple_list_item_1);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {// pozisyonunu alır ve çizer
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.calc_button, parent, false);//VİEWVİ GÖRSEL HALE GETİRRİ INFLATER
}
TextView button = (TextView) convertView;
button.setText(getItem(position));//pozisyonu alır.
return button;
}
}
}
click.class
package com.example.tckcakar.myapplication;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;
/**
* Created by TCKCAKAR on 7.8.2017.
*/
public class click extends MainActivity implements View.OnClickListener {
TextView btn0 = (TextView) findViewById(R.id.textView0);
TextView btn1 = (TextView) findViewById(R.id.textbir);
TextView btn2 = (TextView) findViewById(R.id.textViewiki);
TextView btn3 = (TextView) findViewById(R.id.textViewüç);
TextView btn4 = (TextView) findViewById(R.id.textView4);
TextView btn5 = (TextView) findViewById(R.id.textView5);
TextView btn6 = (TextView) findViewById(R.id.textView6);
TextView btn7 = (TextView) findViewById(R.id.textView2);
TextView btn8 = (TextView) findViewById(R.id.textView1);
TextView btn9 = (TextView) findViewById(R.id.textView3);
TextView btnc = (TextView) findViewById(R.id.textViewC);
TextView topla = (TextView) findViewById(R.id.topla);
TextView cıkar = (TextView) findViewById(R.id.cıkar);
TextView carp = (TextView) findViewById(R.id.textViewyıldız);
TextView böl = (TextView) findViewById(R.id.bölme);
TextView sonuç = (TextView) findViewById(R.id.textVieweşit);
TextView edit = (TextView) findViewById(R.id.edit);
{
btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btnc.setOnClickListener(this);
topla.setOnClickListener(this);
cıkar.setOnClickListener(this);
carp.setOnClickListener(this);
böl.setOnClickListener(this);
sonuç.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.textbir:
edit.setText(edit.getText() + "1");
case R.id.textViewiki:
edit.setText(edit.getText() + "2");
case R.id.textViewüç:
edit.setText(edit.getText() + "3");
case R.id.textView4:
edit.setText(edit.getText() + "4");
case R.id.textView5:
edit.setText(edit.getText() + "5");
case R.id.textView6:
edit.setText(edit.getText() + "6");
case R.id.textView7:
edit.setText(edit.getText() + "7");
case R.id.textView8:
edit.setText(edit.getText() + "8");
case R.id.textView3:
edit.setText(edit.getText() + "9");
case R.id.textViewC:
try {
ilkdeger = 0;
sonuc = 0;
edit.setText("");
} catch (Exception btnC) {
}
case R.id.topla:
try {
if (!edit.getText().toString().isEmpty()) {
ilkdeger = Float.parseFloat(edit.getText().toString());
edit.setText("");
islem = "Toplama";
}
} catch (Exception C) {
}
case R.id.cıkar:
try {
if (!edit.getText().toString().isEmpty()) {
ilkdeger = Float.parseFloat(edit.getText().toString());
edit.setText("");
islem = "Çıkarma";
}
} catch (Exception C) {
}
case R.id.textViewyıldız:
try {
if (!edit.getText().toString().isEmpty()) {
ilkdeger = Float.parseFloat(edit.getText().toString());
edit.setText("");
islem = "Çarpma";
}
} catch (Exception C) {
}
case R.id.bölme:
try {
if (!edit.getText().toString().isEmpty()) {
ilkdeger = Float.parseFloat(edit.getText().toString());
edit.setText("");
islem = "Bölme";
}
} catch (Exception C) {
}
case R.id.textVieweşit:
if (!edit.getText().toString().isEmpty() && !String.valueOf(ilkdeger).equals("0")) {
if (islem.equals("Toplama")) {
sonuc = ilkdeger + Float.parseFloat(edit.getText().toString());
} else if (islem.equals("Çıkarma")) {
sonuc = ilkdeger - Float.parseFloat(edit.getText().toString());
} else if (islem.equals("Çarpma")) {
sonuc = ilkdeger * Float.parseFloat(edit.getText().toString());
} else if (islem.equals("Bölme")) {
sonuc = ilkdeger / Float.parseFloat(edit.getText().toString());
}
edit.setText(String.valueOf(sonuc));
ilkdeger = 0;
sonuc = 0;
} else {
edit.setText("Değer giriniz");
}
if (R.id.textViewC == v.getId()
) {
ilkdeger = 0;
sonuc = 0;
edit.setText("");
}
}
}
}
activy_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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.tckcakar.myapplication.MainActivity">
<TextView
android:layout_width="0dp"
android:layout_height="495dp"
tools:layout_editor_absoluteY="-265dp"
tools:layout_editor_absoluteX="98dp"
android:id="@+id/edit"/>
<GridView
android:id="@+id/gridView1"
android:layout_width="82dp"
android:layout_height="495dp"
android:columnWidth="50dp"
android:gravity="center"
android:numColumns="4"
tools:layout_editor_absoluteX="122dp"
tools:layout_editor_absoluteY="60dp">
</GridView>
</RelativeLayout>
calc_button.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="300dp"
android:weightSum="1">
<TextView
android:id="@+id/textView2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="0.05"
android:background="@color/colorAccent"
android:gravity="center"
android:text="7"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/textView7"
android:clickable = "true"/>
<TextView
android:id="@+id/textView1"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="8"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/textView2"
android:clickable = "true"/>
<TextView
android:id="@+id/textView3"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="9"
android:textAlignment="center"
android:layout_toEndOf="@+id/textView1"
android:layout_above="@+id/textView5"
android:clickable = "true"/>
<TextView
android:id="@+id/textViewC"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="C"
android:textAlignment="center"
android:layout_below="@+id/textView8"
android:layout_toEndOf="@+id/textView3"
android:clickable = "true"/>
<TextView
android:id="@+id/textView4"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="4"
android:textAlignment="center"
android:layout_below="@+id/textView2"
android:layout_toEndOf="@+id/textView7"
android:clickable = "true"/>
<TextView
android:id="@+id/textView5"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="5"
android:textAlignment="center"
android:clickable = "true"
android:layout_below="@+id/textView1"
android:layout_toEndOf="@+id/textView2" />
<TextView
android:id="@+id/textView6"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="6"
android:textAlignment="center"
android:layout_above="@+id/textViewiki"
android:layout_alignEnd="@+id/textViewüç"
android:clickable = "true"/>
<TextView
android:id="@+id/bölme"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="/"
android:textAlignment="center"
android:layout_above="@+id/textViewüç"
android:layout_alignEnd="@+id/textViewC"
android:clickable = "true"/>
<TextView
android:id="@+id/textbir"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="1"
android:textAlignment="center"
android:layout_alignBottom="@+id/textViewiki"
android:clickable = "true"/>
<TextView
android:id="@+id/textViewiki"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="2"
android:textAlignment="center"
android:layout_below="@+id/textView5"
android:layout_toEndOf="@+id/textbir"
android:clickable = "true"/>
<TextView
android:id="@+id/textViewüç"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="3"
android:textAlignment="center"
android:layout_below="@+id/textView6"
android:layout_toEndOf="@+id/textView5"
android:clickable = "true"/>
<TextView
android:id="@+id/textViewyıldız"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="*"
android:textAlignment="center"
android:layout_below="@+id/bölme"
android:layout_toEndOf="@+id/textView6"
android:clickable = "true"/>
<TextView
android:id="@+id/textView0"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="0"
android:textAlignment="center"
android:layout_below="@+id/textbir"
android:layout_toEndOf="@+id/textView7"
android:clickable = "true"/>
<TextView
android:id="@+id/textVieweşit"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="="
android:textAlignment="center"
android:layout_toEndOf="@+id/textView0"
android:layout_alignBottom="@+id/textView0"
android:clickable = "true"/>
<TextView
android:id="@+id/cıkar"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="-"
android:textAlignment="center"
android:layout_toEndOf="@+id/textView3"
android:layout_alignBottom="@+id/textView0"
android:clickable = "true"/>
<TextView
android:id="@+id/topla"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="+"
android:textAlignment="center"
android:layout_alignBottom="@+id/textVieweşit"
android:layout_toStartOf="@+id/cıkar"
android:clickable = "true"/>
</RelativeLayout>
Implemantasyon
Bu koddaki OnClickListenerları daha kısa bir şekilde implement etmek istiyorum yani kodun kısalmasını isitiyorum.Bunu interface ile nasıl yapabilirim?
MainActivity.class
package com.example.tckcakar.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements OnClickListener {
private float ilkdeger=0;
private double sonuc=0;
private String islem;
public MainActivity() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView txt= (TextView) findViewById(R.id.txt);
Button c= (Button) findViewById(R.id.sil);
Button btn0= (Button) findViewById(R.id.buton0);
Button btn1= (Button) findViewById(R.id.buton1);
Button btn2= (Button) findViewById(R.id.buton2);
Button btn3= (Button) findViewById(R.id.buton3);
Button btn4= (Button) findViewById(R.id.buton4);
Button btn5= (Button) findViewById(R.id.buton5);
Button btn6= (Button) findViewById(R.id.buton6);
Button btn7= (Button) findViewById(R.id.buton7);
Button btn8= (Button) findViewById(R.id.buton8);
Button btn9= (Button) findViewById(R.id.buton9);
final Button topla= (Button) findViewById(R.id.topla);
Button çıkar= (Button) findViewById(R.id.çıkar);
Button çarp= (Button) findViewById(R.id.çarpma);
Button böl= (Button) findViewById(R.id.bölme);
final Button sonuç= (Button) findViewById(R.id.Sonuç);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+ "1");
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText().toString()+ "2");
}
});
btn3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+ "3");
}
});
btn4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+ "4");
}
});
btn5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+ "1");
}
});
btn6.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+ "6");
}
});
btn7.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+ "7");
}
});
btn8.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+ "8");
}
});
btn9.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+ "9");
}
});
btn0.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
txt.setText(txt.getText()+ "0");
}
});
try {c.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
ilkdeger=0;
sonuc=0;
txt.setText("");
}
});
}
catch (Exception C){
}
try {topla.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(!txt.getText().toString().isEmpty()){
ilkdeger= Float.parseFloat(txt.getText().toString());
txt.setText("");
islem="Toplama";
}
}
});
}
catch (Exception C){
}
try {çıkar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(!txt.getText().toString().isEmpty()){
ilkdeger= Float.parseFloat(txt.getText().toString());
txt.setText("");
islem="Çıkarma";
}
}
});
}
catch (Exception C){
}
try {çarp.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(!txt.getText().toString().isEmpty()){
ilkdeger= Float.parseFloat(txt.getText().toString());
txt.setText("");
islem="Çarpma";
}
}
});
}
catch (Exception C){
}
try {böl.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(!txt.getText().toString().isEmpty()){
ilkdeger= Float.parseFloat(txt.getText().toString());
txt.setText("");
islem="Bölme";
}
}
});
}
catch (Exception C){
}
sonuç.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(!txt.getText().toString().isEmpty() && !String.valueOf(ilkdeger).equals("0")){
if(islem.equals("Toplama")){
sonuc= ilkdeger + Float.parseFloat(txt.getText().toString());
}
else if(islem.equals("Çıkarma")) {
sonuc = ilkdeger - Float.parseFloat(txt.getText().toString());
}
else if(islem.equals("Çarpma")){
sonuc= ilkdeger * Float.parseFloat(txt.getText().toString());
}
else if(islem.equals("Bölme")) {
sonuc = ilkdeger / Float.parseFloat(txt.getText().toString());
}
txt.setText(String.valueOf(sonuc));
ilkdeger=0;
sonuc=0;
}
else{
txt.setText("Değer giriniz");
}
if (R.id.sil == view.getId()
) {
ilkdeger=0;
sonuc=0;
txt.setText("");
}
}
});}
@Override
public void onClick(View view) {
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tckcakar.myapplication.MainActivity">
<TextView
android:id="@+id/txt"
android:layout_width="167dp"
android:layout_height="44dp"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/linear1"
android:layout_marginTop="53dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.073"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.034" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/linear1"
android:layout_below="@+id/txt"
android:layout_alignParentEnd="true"
android:layout_marginTop="11dp">
<Button
android:id="@+id/buton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="1" />
<Button
android:id="@+id/buton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="2"
/>
<Button
android:id="@+id/buton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="3" />
<Button
android:id="@+id/sil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="C" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/linear2"
android:layout_alignTop="@+id/linear1"
android:layout_alignParentStart="true"
android:layout_marginTop="53dp">
<Button
android:id="@+id/buton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="4"
/>
<Button
android:id="@+id/buton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="5"
/>
<Button
android:id="@+id/buton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="6"
/>
<Button
android:id="@+id/topla"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="+" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/linear3"
android:layout_alignTop="@+id/linear2"
android:layout_alignParentStart="true"
android:layout_marginTop="53dp">
<Button
android:id="@+id/buton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="7"
/>
<Button
android:id="@+id/buton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="8"
/>
<Button
android:id="@+id/buton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="9"
/>
<Button
android:id="@+id/çıkar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/linear4"
android:layout_alignTop="@+id/linear3"
android:layout_marginTop="53dp">
<Button
android:id="@+id/buton0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="0"
/>
<Button
android:id="@+id/bölme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="/" />
<Button
android:id="@+id/çarpma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:text="*" />
<Button
android:id="@+id/Sonuç"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="=" />
</LinearLayout>
</RelativeLayout>
TextView Sorunu
İkinci sıraya neden textView ekleyemiyorum?
Main Activity'im
GridView gridView;
String[] dizi = {
"7", "8", "9", "C", "4", "5", "6", "/", "1", "2", "3", "*", "0", "=", "+", "-"
};
@Override
protected void onCreate(javascript:void(0)Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView1);
ArrayAdapter<String> adapter = new MyAdapter(this);
adapter.addAll(dizi);//diziyi adaptöre gönderdik.
gridView.setAdapter(adapter);//gridview ve adaptörü bağladık.
adapter.notifyDataSetChanged();
}
private class MyAdapter extends ArrayAdapter<String> {//adaptörü sınıf olarak oluşturduk.
MyAdapter(@NonNull Context context) {
super(context, android.R.layout.simple_list_item_1);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {// pozisyonunu alır ve çizer
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.calc_button, parent, false);//VİEWVİ GÖRSEL HALE GETİRRİ INFLATER
}
TextView button = (TextView) convertView;
button.setText(getItem(position));//pozisyonu alır.
return button;
}
}
}
Click and drag to move
calc_button.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:id="@+id/textView2"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="7"
android:textAlignment="center"
android:layout_weight="0.05"></TextView>
<TextView
android:id="@+id/textView1"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="8"
android:textAlignment="center"></TextView>
<TextView
android:id="@+id/textView3"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="9"
android:textAlignment="center"></TextView>
<TextView
android:id="@+id/textView"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="C"
android:textAlignment="center"></TextView>
</LinearLayout>