安卓可编辑文本框的使用
文本框作为最简单也是使用最平凡的控件,也说说吧。
来吧,少年,来添加Activity,src文件夹下添加一个EditTextActivity。
View Code
package cn.Kurodo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.util.Log;
public class EditTextActivity extends Activity {
private Button m_button;
private EditText m_editText;
public EditTextActivity() {
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.edittext);
m_button = (Button)findViewById(R.id.editBtn);
m_editText = (EditText)findViewById(R.id.editText);
m_editText.selectAll();
m_button.setOnClickListener(new ButtonListener());
}
class ButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.v("EditText", m_editText.getText().toString());
}
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.util.Log;
public class EditTextActivity extends Activity {
private Button m_button;
private EditText m_editText;
public EditTextActivity() {
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.edittext);
m_button = (Button)findViewById(R.id.editBtn);
m_editText = (EditText)findViewById(R.id.editText);
m_editText.selectAll();
m_button.setOnClickListener(new ButtonListener());
}
class ButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.v("EditText", m_editText.getText().toString());
}
}
}
EditText的布局文件
View Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/edittext" />
<Button android:id="@+id/editBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/editBtn" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/edittext" />
<Button android:id="@+id/editBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/editBtn" />
</LinearLayout>
运行效果如下: