0

我正在嘗試爲木製標牌製作一個定製應用程序。當用戶點擊10 x10大小的標誌時,我想讓編輯文本框更改大小以顯示他們已完成的操作。這可以在android studio上完成嗎? 這是我到目前爲止的代碼,我認爲我所做的是改變了文本的大小,但當按鈕被點擊時它崩潰,所以我不知道。 Java文件:如何根據按下哪個按鈕來更改編輯文本框的實際大小?

public class Letters extends AppCompatActivity { 
    int txtSize = 14; 
    EditText Wood; 
    Button bSize, bSize1, bSize2, bSize3; 

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

     Button bSize = (Button) findViewById(R.id.bSize); 
     Button bSize1 = (Button) findViewById(R.id.bSize1); 
     Button bSize2 = (Button) findViewById(R.id.bSize2); 
     Button bSize3 = (Button) findViewById(R.id.bSize3); 

     bSize.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       if (Wood.getTextSize() == 15) { 
        txtSize--; 
        Wood.setTextSize(txtSize); 
       } else { 
        txtSize += 0; 
        Wood.setTextSize(txtSize); 
       } 
      } 
     }); 
     bSize1.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       if (Wood.getTextSize() == 16) { 
        txtSize--; 
        Wood.setTextSize(txtSize); 
       } else { 
        txtSize += 0; 
        Wood.setTextSize(txtSize); 
       } 
      } 
     }); 

     bSize2.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       if (Wood.getTextSize() == 17) { 
        txtSize--; 
        Wood.setTextSize(txtSize); 
       } else { 
        txtSize += 0; 
        Wood.setTextSize(txtSize); 
       } 
      } 
     }); 


     bSize3.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       if (Wood.getTextSize() == 18) { 
        txtSize--; 
        Wood.setTextSize(txtSize); 
       } else { 
        txtSize += 0; 
        Wood.setTextSize(txtSize); 
       } 
      } 
     }); 

    } 
} 

XML文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_height="match_parent" 
    android:padding="60dp" 
    android:layout_width="match_parent" 
    android:weightSum="1"> 

    <Button 
     android:id="@+id/bSize" 
     android:text="15 x 10 cm " 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="bSize" /> 

    <Button 
     android:id="@+id/bSize1" 
     android:text="20 x 15 cm" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="bSize1" /> 

    <Button 
     android:id="@+id/bSize2" 
     android:text="30 x 7 cm" 
     android:layout_width="103dp" 
     android:layout_height="61dp" 
     android:onClick="bSize2" /> 

    <Button 
     android:id="@+id/bSize3" 
     android:text="30 x 20 cm" 
     android:layout_width="0dp" 
     android:layout_marginBottom="20dp" 
     android:layout_height="160dp" 
     android:onClick="bSize3" 
     android:layout_gravity="center_vertical" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:id="@+id/Wood" 
     android:textSize="40dp" 
     android:layout_alignTop="@+id/button3" 
     android:layout_centerHorizontal="true" 
     android:background="#795548" 
     android:hint="Choose Letters.."/> 

</LinearLayout> 
+0

順便說一句,你應該遵循變量的標準命名約定,所以變量「木」應該開始用小寫字母「木頭」。 – chRyNaN

回答

1

您的應用程序崩潰,可能是因爲NullPointerException異常的。這是因爲你沒有在onCreate方法中初始化你的Wood變量。

你應該做的是這樣的:

Wood = (EditText) findViewById(R.id.Wood); 
Button bSize = (Button) findViewById(R.id.bSize); 
Button bSize1 = (Button) findViewById(R.id.bSize1); 
Button bSize2 = (Button) findViewById(R.id.bSize2); 
Button bSize3 = (Button) findViewById(R.id.bSize3); 

當你的應用程序崩潰,有Android Studio中一logcat的錯誤堆棧跟蹤。堆棧跟蹤顯示了什麼異常導致了崩潰。您可以通過查看logcat日誌輕鬆解決問題。

+0

謝謝。你知道如何改變實際的editText框的大小,而不是其中的文本,當按下按鈕時。謝謝 – 09Emi

+0

試試這些建議:http://stackoverflow.com/questions/4257843/in-android-how-to-create-edittext-of-fixed-width –

+0

按大小你是指editText的寬度和高度? –

0

你永遠不初始化您EditText這是造成NullPointerException

添加此

Wood = (EditText) findViewById(R.id.Wood); 

onCreate方法。

更新:這是你如何通過改變高度和EditText按鈕寬度點擊

final LayoutParams lparams = new LayoutParams(150,50); // First param is Width and second is height 
Wood.setLayoutParams(lparams); 
+0

謝謝。你知道如何改變實際的editText框的大小,而不是其中的文本,當按下按鈕時。謝謝 – 09Emi

+0

@ 09Emi:請參閱我的更新。 – Rohit5k2

+0

您是否知道如何將編輯文本保存爲圖像,然後我可以將它作爲一個在線購物籃稱爲下一頁?謝謝 – 09Emi

相關問題