安卓 常用控件 监听 事件
有勇气的牛排
1084
安卓
2021-10-17 00:47:33
< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
< TextView
android:id="@+id/One"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="One"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="104dp"
tools:layout_editor_absoluteY="116dp" />
< EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="7dp"
android:layout_marginTop="32dp"
android:ems="10"
android:text="输入用户名">
< /EditText>
< EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_marginTop="19dp"
android:ems="10"
android:text="密码" />
< Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_marginLeft="243dp"
android:text="Button" />
< /RelativeLayout>
TextView textView1;
EditText edit1;
EditText edit2;
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = findViewById(R.id.One);
edit1 = (EditText) findViewById(R.id.editText1);
edit2 = (EditText) findViewById(R.id.editText2);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = edit1.getText().toString();
String password = edit2.getText().toString();
textView1.setText(username);
textView1.setTextColor(Color.RED);
textView1.setTextSize(20);
}
});
}
<pre><div class="hljs"><code class="lang-html">< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
< TextView
android:id="@+id/One"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="One"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="104dp"
tools:layout_editor_absoluteY="116dp" />
< EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="7dp"
android:layout_marginTop="32dp"
android:ems="10"
android:text="输入用户名">
< /EditText>
< EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_marginTop="19dp"
android:ems="10"
android:text="密码" />
< Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_marginLeft="243dp"
android:text="Button" />
< /RelativeLayout>
</code></div></pre>
<pre><div class="hljs"><code class="lang-java">TextView textView1;
EditText edit1;
EditText edit2;
Button btn1;
<span class="hljs-meta">@Override</span>
<span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> {
<span class="hljs-built_in">super</span>.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = findViewById(R.id.One);
edit1 = (EditText) findViewById(R.id.editText1);
edit2 = (EditText) findViewById(R.id.editText2);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">View</span>.OnClickListener() {
<span class="hljs-meta">@Override</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onClick</span><span class="hljs-params">(View view)</span> {
<span class="hljs-type">String</span> <span class="hljs-variable">username</span> <span class="hljs-operator">=</span> edit1.getText().toString();
<span class="hljs-type">String</span> <span class="hljs-variable">password</span> <span class="hljs-operator">=</span> edit2.getText().toString();
textView1.setText(username);
textView1.setTextColor(Color.RED);
textView1.setTextSize(<span class="hljs-number">20</span>);
}
});
}
</code></div></pre>
留言