Java – textview confirmation location
•
Java
I'm trying to make an Android code that provides different work for each button
So when the user presses button 1, the textview field provides "you pressed button 1"
My question is about the correct place. I can instantiate textview only once, because the only working place is in the button1 function
Java code for a single button:
public void Button1(View view) { final TextView textView=(TextView)findViewById(R.id.textView2); textView.setText("You clicked the button 1"); }
Total java code
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view,"Replace with your own action",Snackbar.LENGTH_LONG) .setAction("Action",null).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main,menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button,so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public void Button1(View view) { final TextView textView=(TextView)findViewById(R.id.textView2); textView.setText("You clicked the button 1"); } public void Button2(View view) { final TextView textView=(TextView)findViewById(R.id.textView2); textView.setText("You clicked the button 2"); } public void Button3(View view) { final TextView textView=(TextView)findViewById(R.id.textView2); textView.setText("You clicked the button 3"); } public void Button4(View view) { final TextView textView=(TextView)findViewById(R.id.textView2); textView.setText("You clicked the button 4"); } public void Button5(View view) { final TextView textView=(TextView)findViewById(R.id.textView2); textView.setText("You clicked the button 5"); } public void Button6(View view) { final TextView textView=(TextView)findViewById(R.id.textView2); textView.setText("You clicked the button 6"); } }
Total XML code
<?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:id="@+id/RelativeLayoutID" 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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.sherifsaleh.xmllayout.MainActivity" tools:showIn="@layout/activity_main" > <Button android:id="@+id/button3" android:onClick="Button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" android:layout_above="@+id/button6" android:layout_alignLeft="@+id/button6" android:layout_alignStart="@+id/button6" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="Button6" android:text="6" android:id="@+id/button6" android:layout_below="@+id/button2" android:layout_alignLeft="@+id/button9" android:layout_alignStart="@+id/button9" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="9" android:id="@+id/button9" android:onClick="Button9" android:layout_alignTop="@+id/button8" android:layout_toRightOf="@+id/button8" android:layout_toEndOf="@+id/button8" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="8" android:id="@+id/button8" android:layout_below="@+id/button5" android:layout_toRightOf="@+id/button4" android:layout_toEndOf="@+id/button4" android:onClick="Button8"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" android:onClick="Button2" android:id="@+id/button2" android:layout_above="@+id/button5" android:layout_toRightOf="@+id/button1" android:layout_toEndOf="@+id/button1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="Button5" android:text="5" android:id="@+id/button5" android:layout_below="@+id/button1" android:layout_toRightOf="@+id/button1" android:layout_toEndOf="@+id/button1" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:onClick="Button1" android:layout_above="@+id/button4" android:layout_alignLeft="@+id/button4" android:layout_alignStart="@+id/button4" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4" android:id="@+id/button4" android:layout_above="@+id/button7" android:layout_alignLeft="@+id/button7" android:layout_alignStart="@+id/button7" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="7" android:id="@+id/button7" android:layout_alignParentBottom="true" android:layout_alignLeft="@+id/textView2" android:layout_alignStart="@+id/textView2" android:layout_marginBottom="90dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textIsSelectable="true" android:text="New Text" android:id="@+id/textView2" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="80dp" android:textSize="50dp"/> </RelativeLayout>
Solution
Try this
public class MainActivity extends AppCompatActivity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.textView2); .... }
And your button click method is
public void Button1(View view) { textView.setText("You clicked the button 1"); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码