Android Studio Button 事件
刚学习的菜鸟求代码。一个Button ,一个EditText,两个TextView。
当 EditText="1",点击Button ,TextView1="20";TextView2="30"。
当 EditText="3",点击Button ,TextView1="25";TextView2="35"。
MainActivity.java 代码 该怎么写?
package com.example.administrator.a3d; import android.support.v7.app.AppCompatActivity; import android.widget.Button; import android.widget.TextView; import android.widget.EditText; import android.view.View; import android.os.Bundle; public class MainActivity extends AppCompatActivity { private TextView textView1; private TextView textView2; private EditText editText1; private Button button1; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = (TextView)findViewById(R.id.textView1); textView2 = (TextView)findViewById(R.id.textView2); editText1 = (EditText)findViewById(R.id.editText1); button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { if("1".equals(editText1.getText().toString().trim())) { textView1.setText("20"); textView2.setText("30"); } if("3".equals(editText1.getText().toString().trim())) { textView1.setText("25"); textView2.setText("35"); } } }); } }
[此贴子已经被作者于2016-5-24 23:00编辑过]