2016-02-17 61 views
-2

我有一個的EditTextTextView的按鈕。用戶在EditText中鍵入一個數字。然後,我希望用戶點擊該按鈕後將其除以100,並在TextView中顯示答案。Android的 - 做一個簡單的計算

這裏是我的佈局(activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<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=".MainActivity"> 


<EditText 
    android:id="@+id/tv_numb" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="77dp" 
    android:hint="Provide a number"/> 

<Button 
    android:id="@+id/btn_doCalc" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Calculate" 
    android:layout_below="@+id/tv_numb" 
    android:layout_centerHorizontal="true" /> 

<TextView 
    android:id="@+id/tv_answer" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/btn_doCalc" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="50dp" 
    android:textSize="30sp" 
    android:text="Answer" /> 

MainActivity.java

public class MainActivity extends ActionBarActivity { 

private EditText editTextNumb; 
private Button buttonCalc; 
private TextView tvAnswer; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    editTextNumb = (EditText) findViewById(R.id.tv_numb); 
    buttonCalc = (Button) findViewById(R.id.btn_doCalc); 
    tvAnswer = (TextView) findViewById(R.id.tv_answer); 

    //I think this is where my problem is------- 
    final int result = (editTextNumb.getText().toString())/100; 
    // 

     buttonCalc.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       tvAnswer.setText(result); 

      }}); 
    } 

} 

任何幫助,將不勝感激。

+1

幫忙做什麼?你沒有向我們展示你的代碼,也沒有解釋你有什麼問題。在沒有任何關於你卡住的背景的情況下,我們不可能解除你的束縛。 – azurefrog

+1

歡迎來到Stack Overflow!請參閱[如何提問](http://stackoverflow.com/help/how-to-ask)和[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 – radoh

回答

3
Button mButton; 
EditText mEdit; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mButton = (Button)findViewById(R.id.button); 
    mEdit = (EditText)findViewById(R.id.edittext); 

    mButton.setOnClickListener(
     new View.OnClickListener() 
     { 
      public void onClick(View view) 
      { 
       try 
       { 
       int result = Integer.parseInt(mEdit.getText().toString())/100; 
       // show it to them 
       Log.v("EditText", "result is "+result); 

       Toast.makeText(getApplicationContext(),"result is "+result,  
           Toast.LENGTH_LONG).show(); 
       } 
       catch (final NumberFormatException e) 
       { 
        // tell them they didnt enter a valid number 
       Toast.makeText(getApplicationContext(),"Please Enter a valid number",  
           Toast.LENGTH_LONG).show(); 
       } 
      } 
    }); 
} 
+0

我用Log.v(「EditText」,result)得到一個錯誤「Wrong 2nd argument type」; –

+0

@ H.Brooks請檢查編輯 –

+0

啊太好了,我怎樣才能在屏幕上顯示它,目前它只顯示在日誌中,而不是模擬器。 –