TextEditの無入力の判定について

2,200 views
Skip to first unread message

リックス

unread,
Dec 20, 2008, 1:46:57 PM12/20/08
to 日本Androidの会
お世話になってます
リックスです

TextEditで何も入力を入れなくてボタンを押したときの判定の仕方でつまずいています

判定の仕方で4パターンほど考えましたが、どれもエラーになってしまいます

public void onClick(View v)
{
//部品の定義部分
TextView result_screen = new TextView(this);
TextView alaert_screen = new TextView(this);
EditText Height = new EditText(this);
EditText Weight = new EditText(this);
Dialog result = new Dialog(this);
Dialog alart = new Dialog(this);

    //考えたパターン(i)
     if(Height.toString().length() == 0 || Weight.toString().length()
== 0) -----(i)
   {

     }

//考えたパターン(ii),(iii)
 buf_height = Integer.parseInt(Height.getText().toString());
 buf_weight = Integer.parseInt(Weight.getText().toString());
if(buf_height == 0x00 || buf_weight == 0x00) -------(ii)
{
}
if(Height.toString()== null || buf_weight == null)
-------(iii)
{
}
if("".equals(buf_height) || "".equals(buf_weight))-------
(iV)
{
}



どのように判定すればいいのでしょうか?
よろしくお願いします

Hideo Kinami

unread,
Dec 20, 2008, 4:18:46 PM12/20/08
to android-g...@googlegroups.com
木南です。

この勘違い僕もやりました!
EditTextのgetTextの戻り値は、Editableなので、中の文字列を取り出すのは、
そのEditableのtoStringですね。そして、その長さ得るのは、toString().length()だと思います。

そうすると、テキストボックスが空かどうかは、

EditText Height = new EditText(this);

if ( Height.getText().toString().lenght() == 0 ) {
// 空の処理
}

でよいのでは?
質問の意図が違っていたら、教えてください!

ちなみに、確認用に書いたプログラムです。

--- HelloEditableActivity.java ----
package jp.hews.helloeditable;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class HelloEditableActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.button_id);
b.setOnClickListener(mButtonListener);
}

private View.OnClickListener mButtonListener =
new View.OnClickListener() {
public void onClick(View v) {
EditText e = (EditText)findViewById(R.id.edit_text_id);
// 入力された文字列の長さを調べる
int len = e.getText().toString().length();
Toast.makeText(
getApplicationContext(),
"len = " + len,
Toast.LENGTH_LONG).show();
}
};
}

--- main.xml ----
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_text_id"></EditText>
<Button
android:id="@+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>


# その意味では、リックスさんのやりかた、かなり、いい線いっているのですが、ちょっとだけ、通りすぎちゃったんですかね?

--
Hideo




2008/12/21 3:46 リックス <lic...@gmail.com>:
--
木南 英夫 ( http://d.hatena.ne.jp/hkinami/ )
Reply all
Reply to author
Forward
0 new messages