以下のコードを複数のクラスに分割して実行したいのですがどのようにしたら良いのでしょうか?
静的なメソッドではないので、仮にocbというクラスを作り、ocb.onClickButton();のようにしてもうまくいきません
import android.graphics.Color; import android.graphics.drawable.Drawable; import android.graphics.drawable.GradientDrawable; import android.graphics.drawable.LayerDrawable; import android.os.Build; import android.support.annotation.RequiresApi; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) public void onClickButton(View view) { // ボタンを生成 Button button = new Button(this); EditText editText; editText = (EditText) findViewById(R.id.editText); String text = editText.getText().toString(); button.setTranslationX(200); button.setTranslationY(1000); LinearLayout.LayoutParams size = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT ); // テキスト button.setText(text); // タイトルの色 button.setTextColor(Color.rgb(80, 30, 30)); // 背景色 button.setBackgroundColor(Color.rgb(200, 200, 200)); GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setStroke(2, Color.rgb(100, 100, 100)); // ボーダーの幅、色 gradientDrawable.setColor(Color.rgb(230, 180, 180)); // 背景色 LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] { gradientDrawable }); layerDrawable.setLayerInset(0, -2, -2, 0, 0); button.setTag(123); button.setOnClickListener(this); this.addContentView(button, size); } public void onClick(View view) { String tag = view.getTag().toString(); if (tag.equals("123")) { new AlertDialog.Builder(this) .setTitle("eri") .setMessage("動的に作ったボタンがクリックされました!") .setPositiveButton("OK", null) .show(); } } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。